这就是WordPress API,用于此的适当工具是register_activation_hook
插件激活后立即触发,或after_setup_theme
如果你是为了一个主题而做这项工作。
下面是一个使用示例,代码应该放在主插件加载程序文件中,或者如果放在子目录中,则提供__FILE__
主文件中的属性:
add_action( "admin_init", function(){
if ( get_option( $opt_name = "se_show_my_plugin_wizard_notice" ) ) {
delete_option( $opt_name );
add_action( "admin_notices", "se_wizard_notice" );
} return;
});
/**
* Check if user has completed wizard already
* if so then return true (don\'t show notice)
*
*/
function se_wizard_completed() {
return false;
}
function se_wizard_notice() {
if ( se_wizard_completed() ) return; // completed already
?>
<div class="updated notice is-dismissible">
<p>Welcome to my plugin! You\'re almost there, but we think this wizard might help you setup the plugin.</p>
<p><a href="admin.php?page=my_plugin_wizard" class="button button-primary">Run wizard</a> <a href="javascript:window.location.reload()" class="button">dismiss</a></p>
</div>
<?php
}
register_activation_hook( __FILE__, function() {
update_option( "se_show_my_plugin_wizard_notice", 1 );
});
希望这有帮助。