0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

wordpressのregister_activation_hookで詰まった

Posted at

自作プラグインを作っていたら色々詰まったのでメモ。
ドキュメントを参考にしながらregister_activation_hookに呼び出される関数内で新規テーブルを作ろうとしたが作成されなかった。

失敗例

add_action('init', 'init');
function Init()
{
    register_activation_hook(__FILE__, 'plugin_activation');
}

ざっくりこんな記述をしていた。
何が悪かったのかというとinit Hookで呼び出される関数内でregister_activation_hookを呼び出していた所。
英語のドキュメントを見ると分かりやすいが、

Registering the hook inside the ‘plugins_loaded’ hook will not work. You can’t call register_activation_hook() inside a function hooked to the 'plugins_loaded' or 'init' hooks (or any other hook). These hooks are called before the plugin is loaded or activated.

そもそもhook内でactivation_hookを呼び出せないようだ。
なのでactivation_hookをhookの外に出せばOK

add_action('init', 'init');
register_activation_hook(__FILE__, 'plugin_activation');
function Init()
{
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?