请注意$file 在里面register_activation_hook( $file, $callback ), 应该等于主插件文件的路径,但当您将其作为__FILE__ 那就不一样了!这意味着永远不会调用您的回调。
我还建议在函数名称前加前缀,以避免可能的名称冲突或使用名称空间。
法典中的更多内容here.
Update
深入了解函数的定义有助于提供信息:
/**
* Set the activation hook for a plugin.
*
* When a plugin is activated, the action \'activate_PLUGINNAME\' hook is
* called. 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/plugins/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/plugins/sample.php the name of this hook will be
* \'activate_sample.php\'.
*
* @since 2.0.0
*
* @param string $file The filename of the plugin including the path.
* @param callable $function The function hooked to the \'activate_PLUGIN\' action.
*/
function register_activation_hook($file, $function) {
$file = plugin_basename($file);
add_action(\'activate_\' . $file, $function);
}
这是对应的
do_action 在中调用插件acitvation
activate_plugin() 功能:
/**
* Fires as a specific plugin is being activated.
*
* This hook is the "activation" hook used internally by register_activation_hook().
* The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
*
* If a plugin is silently activated (such as during an update), this hook does not fire.
*
* @since 2.0.0
*
* @param bool $network_wide Whether to enable the plugin for all sites in the network
* or just the current site. Multisite only. Default is false.
*/
do_action( "activate_{$plugin}", $network_wide );
Example
让我们通过一个真正的插件来了解这一点。
因此,以WooCommerce插件为例,该插件具有主插件文件:
/full/path/to/wp-content/woocommerce/woocommerce.php
它在哪里
defines:
register_activation_hook( __FILE__, array( \'WC_Install\', \'install\' ) );
在哪里
$file 是
__FILE__ 或
/full/path/to/wp-content/woocommerce/woocommerce.php
然后线路:
$file = plugin_basename( $file );
成为:
$file = plugin_basename( \'/full/path/to/wp-content/woocommerce/woocommerce.php\' );
这就产生了:
$file = \'woocommerce/woocommerce.php\';
然后动态挂钩:
add_action(\'activate_\' . $file, $function);
生成:
add_action(\'activate_woocommerce/woocommerce.php\', $function);
因此,如果WooCommerce将注册激活挂钩放在一个特殊文件中,例如。
/full/path/to/wp-content/woocommerce/include/activation.php
那么这就是
__FILE__ 我们将注册以下操作:
add_action(\'activate_woocommerce/include/activation.php\', $function);
但是没有
do_action() 打电话叫那个。
相反,他们可能存储主插件文件路径,以便在其他地方使用,就像他们在主插件文件中所做的那样here:
$this->define( \'WC_PLUGIN_FILE\', __FILE__ );
希望有帮助!