我想在用户尝试激活我的插件时显示一条消息。此消息将询问他是否真的想激活插件,或者如果他改变主意,取消激活。我如何才能做到这一点?这是警告消息的代码,仅供参考。
---更新的代码---
register_activation_hook( __FILE__, \'on_activation\' );
function on_activation() {
// Add the admin notice:
add_action( \'admin_notices\', \'on_activation_note\' );
// Then you should check the DB option:
$plugins = get_option( \'active_plugins\' );
// Do all the checks from the confirmation message
if ( !in_array(__FILE__, $plugins) ) {
unset( $plugins[ dirname( __FILE__ ) ] );
update_option( \'active_plugins\', $plugins );
}
}
function on_activation_note() {
global $pagenow;
if ( $pagenow == \'plugins.php\' ) {
ob_start(); ?>
<div id="message" class="error">
<p><strong>Aviso</strong></br>
Nulla vitae elit libero, a pharetra augue. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.</p>
<p><span><a class="button" href="">Cancelar</a></span>
<span><a class="button" href="">Continuar</a></span></p>
</div>
<?php
echo ob_get_clean();
}
}
SO网友:kaiser
您可以阅读更多有关activation on this answer.
基本上,您需要将函数挂接到register_activation_hook()
- 假设这是来自主插件文件夹,而不是子文件夹:
register_activation_hook( __FILE__, \'on_activation\' );
function wpse65190_on_activation()
{
// Add an admin notice:
add_action( \'admin_notices\', \'wpse65190_on_activation_note\' );
// Then you should check the DB option:
$plugins = get_option( \'active_plugins\' );
if ( ! in_array( dirname( __FILE__ ), $plugins )
{
unset( $plugins[ dirname( __FILE__ ) ] );
update_option( \'active_plugins\', $plugins );
}
}
function wpse65190_on_activation_note()
{
// Add your note here: Maybe a form?
}
就这么简单。你只需要填补空白。如果您有完整的工作示例,请使用您的工作代码更新此答案。谢谢