I have the following code in my plugin:

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
}

However when I activate the plugin I don’t get any notice. I’ve tried using update_option and get_option but I had no luck with that either.

What’s the correct and best way to achieve this?

UPDATE
I tried the transient thing like this:

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' );
    }
}

But it still didn’t work.

UPDATE 2
I had a typo in the transient part. It worked and I’ll post it as an answer.

1 Answer
1

As @MohammadLimon said I need to use the Transients API. The following code worked:

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' );
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *