How to Display Notice in Admin Panel on Plugin Activation? Add Notice to WordPress admin – Easy

In this artical, we will see how to display notice in admin panel on plugin activation.

We already know about the admin_notices hook in wordpress that used to add notices in wordpress admin pannel. In this artical, we will see how to display notice in admin panel on plugin activation.

Problem with Traditional Approach

Before delving into the solution, let’s address the conventional method of adding notices using the admin_notices hook. While effective, this approach presents a drawback – the notice appears every time the admin panel loads, potentially causing user annoyance or confusion with repeated messages.

function plugin_activation_notice() {
    ?>
    <div class="notice notice-success is-dismissible">
        <p><?php esc_html_e( 'Custom notice message.', 'plugin-text-domain' ); ?></p>
    </div>
    <?php
}
add_action('admin_notices', 'plugin_activation_notice');

This notice will show in admin pannel every time when the wp admin loads.

Display Notice in Admin Panel on Plugin Activation

To overcome this limitation and provide a more user-friendly experience, we utilize the register_activation_hook function coupled with transient data. Transients are temporary pieces of data stored in the WordPress database with an expiration time. Leveraging this mechanism allows us to display a notice only upon plugin activation, ensuring timely and relevant communication without inundating users with repetitive messages.

Now, we are going to see how can we display the admin notice when the plugin activates.

Here is the code:

register_activation_hook(__FILE__, 'measurement_master_activate');
function measurement_master_activate()
{
    // Create transient data for activation notice
    set_transient('measurement-master-activation-notice', true, 5);
}

// Add admin notice
add_action('admin_notices', 'measurement_master_activate_admin_notice');

/**
 * Admin Notice on Activation.
 */
function measurement_master_activate_admin_notice() {
    // Check transient, if available display notice
    if (get_transient('measurement-master-activation-notice')) {
        ?>
        <div class="updated notice is-dismissible">
            <p><?php esc_html_e('Enable the measurement settings plugin from Measurement Master settings page.', 'plugin-text-domain'); ?></p>
        </div>
        <?php
        // Delete transient, only display this notice once
        delete_transient('measurement-master-activation-notice');
    }
}

Add this code into your plugin file, this will show the custom message in you wordpress admin pannel when the plugin gets activated.

Implementing these best practices not only improves user engagement but also reflects positively on your plugin’s overall quality and user satisfaction.

Also read: How to Change Add to Cart Button Text in Woocommerce

Leave a Comment

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

Scroll to Top