1

I am interested in developing a Wordpress plugin. I've noticed that, after activating other Wordpress plugins, they'll display a "You're almost done, just run the wizard" message at the top of every admin page. (See screenshot for one example of this)

enter image description here

If there officially support for this some Wordpress api and/or system? Or is this something crafy the developers of this plugin have done? (The plugin is WooCommerce -- mentioned only in case the specific plugin matters, this is not an (off topic) WooCommerce question)

Regardless of the answer to the above, how can I achieve this effect in my own plugins?

Thank you!

2

That's WordPress API and the proper tool to use for this is register_activation_hook which fires right after your plugin activation, or after_setup_theme if you were doing this work for a theme.

Here's an example use, the code should be placed in the main plugin loader file, or if in a sub-directory then provide a path to __FILE__ property from the main 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 );
});

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.