Show admin notice only once upon plugin activation

Hi,

How is it possible to show an admin_notices only once upon plugin activation? So when the user leave the plugin.php page or closes the admin notice, then it should not be shown again.

Sincerely,

Mika

  • Vaughan
    • Ex Staff

    Hi @mika,

    Hope you’re well?

    The admin notice shouldn’t be displayed if the user has clicked the dismiss button to hide the notice, however I don’t think there’s actually any other way of doing this.

    Could I ask which admin notice you are referring to though, maybe a screenshot. It would depend on the plugin as to how the notice is configured I think, but i’m not 100% on that.

    Let me just ask one of my colleagues @Ashok for some of his expertise on this.

    Hope this helps

  • Mika
    • The Bug Hunter

    Hey Vaughan!

    Yep, much better than I deserve! I hope you are doing good as well :slight_smile:

    You can see my code here:

    /* add a quick link named "Instillinger" on the plugin activation page */
    add_action( 'admin_notices', 'rpfs_admin_notice' );
    add_action( 'network_admin_notices', 'rpfs_admin_notice' ); // also show message on multisite
    function rpfs_admin_notice() {

    if( is_plugin_active( 'plugin-test/plugin-test.php' ) ) {

    global $pagenow;

    if( $pagenow == 'plugins.php' ){

    deactivate_plugins ( 'another-test-plugin/another-test-plugin.php' );

    if ( current_user_can( 'install_plugins' ) ) {

    echo '<div id="error" class="error notice is-dismissible"><p>Some error message</div>';

    }
    }
    }
    }

    It appear upon the plugin activation, however, it does not disappear from the plugin.php page after another page load or by clicking on the dismiss button.

    Looking forward to hear from Ash :smiley:

    Sincerely,

    Mika

  • Mika
    • The Bug Hunter

    Hi,

    It is vert strange but now it seems to be working. I have no idea why it didn’t work before, but I guess it is good!

    However, do I modify a standard admin notification (instead of adding an additional notification)?

    Sincerely,

    Mika

  • Ash
    • Code Norris

    Hello @Mika

    I hope you are well today.

    There is no standard admin notification. Every notice has some purpose. I can see you have hooked to admin_notices and this is perfectly fine, in fact this is standard. When you need to show a notice to the user, use your own function and hook to admin_notices or/and network_admin_notices. It is perfectly fine :slight_smile:

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers

    Ash

  • Mika
    • The Bug Hunter

    Hey Ash.

    Whenever I save the options on on my custom_settings_page, it gives me an message that the settings has been changed. How can I hook into that message? Is there a filter?

    The form look like this:

    <form method="post" action="options.php">

    <?php settings_fields( 'mik_settings_group' );
    do_settings_sections( mik_settings' );
    submit_button(); ?>

    </form>

    Sincerely,

    Mika

  • Ash
    • Code Norris

    Hello @Mika

    I have just checked the wordpress source code and unfortunately there is no hook that you can use, you have to keep that message.

    Another option is, you can use your custom page instead of settings API, where you will subsite the form in same page and you can save the data using update_option function.

    In that case, you can show your custom message as notice. Also, you won’t need admin_notice in this case.

    Use add_menu_page function to register a page. You can add_options_page too.

    In that page you can add your html form as simple html without settings API and submit the form in same page.

    And then, save the data and show the nag message :slight_smile:

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers

    Ash

  • Mika
    • The Bug Hunter

    Hi Ask.

    Thanks for your great answer.

    I really want to stick to the settings API, so I might not be able to accomplish what you are suggestions ( although it is a good idea ). In fact, it is pretty strange that such a hook isn’t available considering how it will help many developers to show a error or success message, staying with the default WP styling.

    Sincerely,

    Mika

  • Ash
    • Code Norris

    Hello @Mika

    If you say styling, you can change that. You can write your own css to change any admin styling and hook with admin_head.

    But to change that message, no there is no hook available unless I am missing something. I checked the source code yesterday and found no hook in there unfortunately :slight_frown:

    Cheers

    Ash

  • Mika
    • The Bug Hunter

    Hi Ask.

    Thanks a lot for your reply and research!

    I might want to suggest this is as a core feature since it will make it easier for developers to make use of the inherent WP notification system!

    Sincerely,

    Mika :slight_smile:

  • Mika
    • The Bug Hunter

    Hey Ash.

    I hope you are doing good today :slight_smile:

    I actually went ahead and hid the default message with css using the admin_head hook. So far, so good. However, now I am trying to add a custom admin notice to show instead.

    I could of course use:

    /* Success message after the users options has been saved */
    add_action('admin_notices', 'my_admin_notice');
    function my_admin_notice(){

    if( $_GET['page'] == 'my_page' && $_GET['settings-updated'] == 'true' ) {

    echo '<div id="updated" class="updated notice is-dismissible"><p><strong>My message.</strong></p></div>';

    }
    }

    This works. However, it shows this error: Notice: Undefined index: settings-updated in …/wp-content/plugins/my-plugin/my-plugin.php on line 67. It disappears when I update the button ( and my custom admin notice is shown ).

    However, I do not want to show that error. By the way, it also shows on all the other pages.

    Hope you are can help out Ash :slight_smile:

    Sincerely,

    Mika

  • Vaughan
    • Ex Staff

    Hi Mika,

    You are getting that error because settings-updated is not actually defined (or set)

    So with a little change to your function.

    /* Success message after the users options has been saved */
    add_action('admin_notices', 'my_admin_notice');
    function my_admin_notice() {
    if( $_GET['page'] == 'my_page' ) {
    if( isset($_GET['settings-updated']) ) {
    echo '<div id="updated" class="updated notice is-dismissible"><p><strong>My message.</strong></p></div>';
    } else {
    echo '<div id="error" class="updated notice is-dismissible"><p><strong>My error message.</strong></p></div>';
    }
    }
    }

    Hope this helps

  • Mika
    • The Bug Hunter

    Hey Vaughan.

    Make sense, thanks a lot.

    It did seem to solve the issue on my settings page, however, I am still getting the following error on all the other pages: “Notice: Undefined index: page in …/wp-content/plugins/my-plugin/my-plugin.php on line 69”

    Sincerely,

    Mika

  • Mika
    • The Bug Hunter

    Vaughan, you lead me in the right direction:

    I just changed if( $_GET['page'] == 'my_page' ) { toif( isset($_GET['page']) && $_GET['page'] == 'my_page' ){and it works perfectly now :slight_smile:

    Thanks a lot!!

    Sincerely,

    Mika