How to Add Custom Admin Notices in WordPress

How to Add Custom Admin Notices in WordPress

Have you ever desired to know how to add admin notices in wordpress. However, the admin noticed are used by WordPress themes, plugins and, core to show the warnings, notices, and important on screens information to users.

You have to inform the users some faults that may have advance or simply send out a notification for an action performed.

Why and When to Use Admin Notices in WordPress?

There the use of admin notices in wordpress is to active users about errors, warnings, and success messages. The particular site owners, plugin authors, and theme developers can also use admin notices. If you are working on a website for clients who are not natural with WordPress, then you can add admin notices to display helpful advice over their WordPress admin area. They can be really disturbing and may ruin the WordPress practice for your users.

Method 1: Add Custom Notices in WordPress Manually:-

Here method requires you to add code to your WordPress site. If you have forget it  added code previously, then take a look at our guide on pasting snippets from the web into WordPress.

If you need to add this code to your theme’s functions.php file or a site-specific plugin.

 

1

2

3

4

5

6

7

8

9

function general_admin_notice(){

    global $pagenow;

    if ( $pagenow == ‘options-general.php’ ) {

         echo ‘<div class=”notice notice-warning is-dismissible”>

             <p>This notice appears on the settings page.</p>

         </div>’;

    }

}

add_action(‘admin_notices’, ‘general_admin_notice’);

 

The  code displays a notice on the settings page with a yellow border and a button to close the notice. This is how it will appear on your site:

Then  you study the code, you will notice that we have used $pagenow variable to detect the current page. Later that we added the action that checks if the current page meets the page where we want to display the notice.

If it does, then we show the notice protected in a <div> element. This div element uses CSS classes already defined in the WordPress admin stylist for particular type of notices.

Here is how you will do that:-

function author_admin_notice(){

global $pagenow;

if ( $pagenow == ‘index.php’ ) {

$user = wp_get_current_user();

if ( in_array( ‘author’, (array) $user->roles ) ) {

echo ‘<div class=”notice notice-info is-dismissible”>

<p>Click on <a href=”edit.php”>Posts</a> to start writing.</p>

</div>’;

}

}

}

add_action(‘admin_notices’, ‘author_admin_notice’);

 

function author_admin_notice(){

global $pagenow;

if ( $pagenow == ‘index.php’ ) {

$user = wp_get_current_user();

if ( in_array( ‘author’, (array) $user->roles ) ) {

echo ‘<div class=”notice notice-info is-dismissible”>

<p>Click on <a href=”edit.php”>Posts</a> to start writing.</p>

</div>’;

}

}

}

add_action(‘admin_notices’, ‘author_admin_notice’);

 

 

Feel free to system with different circumstances filters, and hooks to play with admin notices.

Method 2: Add Admin Notices Using a WordPress Plugin:-

Then  method is clean as it does not lack you to add code. After all, it is not as flexible as the custom code method.

If anything you need to do is install and activate the KJM Admin Notices plugin. For more particulars, see our step by step guide on how to install a WordPress plugin.

you need to stay Settings » KJM Admin Notices page to create plugin settings. If you need to check the option to enable KJM Admin Notices. The next  benefit adds a custom post type where you can add and edit your custom admin notices. Visit Notices » Add Notice page. You will see a screen much like the WordPress post edit screen.

 

Next you need to select the user roles which will see this notice. KJM Admin Notices grant you to conduct your custom admin notices without writing any code. You can clean  or unpublish notices that you don’t want to display any further.

Conclusion-we help you how to learn How to Add Custom Admin Notices in WordPress. Here all step can define in simple language. For any query visit this site:- https://www.wpglobalsupport.com/blog/

Leave a Reply

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