无法在插件激活时添加管理员通知

时间:2016-10-02 作者:Shiv

我的插件中有以下代码:

register_activation_hook( __FILE__, \'sp_subscriber_check_activation_hook\' );
function sp_subscriber_check_activation_hook() {
    add_action( \'admin_notices\', \'sp_subscriber_check_activation_notice\' );
}
function sp_subscriber_check_activation_notice(){
    ?>
    <div class="updated notice is-dismissible">
        <p>If you are using a caching plugin like W3TC or WP Super Cache make sure to make any pages you wish to protect an exception to the caching!.</p>
    </div>
    <?php
}
然而,当我激活插件时,我没有得到任何通知。我试过使用update_optionget_option 但我也没有运气。

实现这一目标的正确和最佳方法是什么?

UPDATE我试过这样的瞬间:

register_activation_hook( __FILE__, \'sp_subscriber_check_activation_hook\' );
function sp_subscriber_check_activation_hook() {
    set_transient( \'mp-admin-notice-activation\', true, 5 );
}

add_action( \'admin_notices\', \'sp_subscriber_check_activation_notice\' );
function sp_subscriber_check_activation_notice(){
     if( get_transient( \'fmp-admin-notice-activation\' ) ){
        ?>
        <div class="updated notice is-dismissible">
            <p>If you are using a caching plugin like W3TC or WP Super Cache make sure to make any pages you wish to protect an exception to the caching!</p>
        </div>
        <?php
        delete_transient( \'mp-admin-notice-activation\' );
    }
}
但还是不起作用。

UPDATE 2我在过渡部分有一个拼写错误。它成功了,我会把它作为一个答案贴出来。

1 个回复
SO网友:Shiv

正如@MohammadLimon所说,我需要使用瞬态API。以下代码有效:

register_activation_hook( __FILE__, \'sp_subscriber_check_activation_hook\' );
function sp_subscriber_check_activation_hook() {
    set_transient( \'mp-admin-notice-activation\', true, 5 );
}

add_action( \'admin_notices\', \'sp_subscriber_check_activation_notice\' );
function sp_subscriber_check_activation_notice(){
     if( get_transient( \'mp-admin-notice-activation\' ) ){
        ?>
        <div class="updated notice is-dismissible">
            <p>If you are using a caching plugin like W3TC or WP Super Cache make sure to make any pages you wish to protect an exception to the caching!</p>
        </div>
        <?php
        delete_transient( \'mp-admin-notice-activation\' );
    }
}