プラグインを有効化させるタイミングで何らかの処理を行いたい場合二通りのやり方があって、ひとつは register_activation_hook を使う方法。これは公式マニュアルにも丁寧に解説があって使いやすい。
もうひとつは、そのマニュアルを読んで知ることになったわけですが、activate_{$PLUGIN_NAME} という名前のアクションフックに引っ掛ける方法。マニュアルにはこちらはあまりお勧めではないという記述がされていますが興味がわいたので、このフックはどういった値なのか調べました。
すると、この $PLUGIN_NAME には、プラグインディレクトリから見たプラグインファイルまでのパスが記述されていました。
たとえば /wp-content/plugins/my-plugin.php なら
activate_my-plugin.php
/wp-content/plugins/my-dir/my-file.php なら
activate_my-dir/my-file.php
という按配です。っていうか、 register_activation_hook のコメントに記述してあった。
/** * Set the activation hook for a plugin. * * When a plugin is activated, the action 'activate_PLUGINNAME' hook is * activated. In the name of this hook, PLUGINNAME is replaced with the name of * the plugin, including the optional subdirectory. For example, when the plugin * is located in wp-content/plugin/sampleplugin/sample.php, then the name of * this hook will become 'activate_sampleplugin/sample.php'. When the plugin * consists of only one file and is (as by default) located at * wp-content/plugin/sample.php the name of this hook will be * 'activate_sample.php'. * * @package WordPress * @subpackage Plugin * @since 2.0 * * @param string $file The filename of the plugin including the path. * @param callback $function the function hooked to the 'activate_PLUGIN' action. */ function register_activation_hook($file, $function) { $file = plugin_basename($file); add_action('activate_' . $file, $function); }
ところでこの二通りの方法の関係は如何に?なぜ二通りもあるのか?ってことですが、上述したように「 プラグイン名とは何ぞや?」ということをいちいち調べなくても済むようにあとで register_activation_hook が用意されたんですかね。
No Comment
You can post first response comment.