Search code examples
wordpresspluginswordpress-plugin-creation

How can I add a plugin message in the wordpress admin interface?


I want to add a notification in the admin interface that tells users "plugin activated" for example, but I don't know which action hook should I use, knowing that I want the message to display not just in the dashboard page, but in all the admin interface pages, like Dashboard, Appearance, Settings...etc, and I want the message to be under the first title of that page, like for example, if we are under Dashboard page, my plugin message should be under the title "Dashboard", if we are under Plugins page, my plugin message should be under the title "Plugins"...etc

Is there any body that can help with this please ?

Thanks.


Solution

  • You can achieve this by using admin_notices of WordPress. This hooks helps you to show message/notice on different pages as per your need.

    You can add the given code to functions.php file of your theme. In the given code you need to replace themetextdomain with your theme Text Domain and in this code we are using admin_notices hook and then we are using $screens_list to compare with the present screen and if that is matched then we will show the '$notice'. You can change $notice and add more screen ID as per your need to screens_list.

    add_action( 'admin_notices', 'custom_notice_regarding_plugin');
    
    function custom_notice_regarding_plugin() {
        $current_screen = get_current_screen();
        $notice         = __('Plugin has been activated successfully.', 'themetextdomain');
        $screens_list   = array(
            'media'
            'plugins',
            'dashboard',
            'page',
            'post'
        );
        
        if ( in_array( $current_screen->id, $screens_list ) ) {
            echo '<div class="notice notice-success is-dismissible"><p>' . $notice . '</p></div>';
        }
    }