How to add custom notice areas in WordPress | Cosmic Skip to main content

How to add custom notice areas in WordPress

SignsAs you may well know, WordPress is a fantastic tool out of the box and has a wide range of features that will enable you to create and maintain a fantastic company website, e-commerce store or blog.

As with any system, there will always be people who will need an extra helping hand when editing the website. A great way of doing this is by adding customised notices and notes for the admins on the backend of the CMS.

There are two different types of notices that you can add, and both will require some PHP file mofifications.

Custom admin notices

First you need to add the following snippet of code to your theme’s functions.php file or a site-specific plugin:

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');

This 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:

WordPress custom admin notice

The snippet of code can also be tweaked to change the type of notification that is displayed. You need to use notice class and then you can add notice-error, notice-warning, notice-success, or notice-info.

Optionally, you can use is-dismissible class which adds a button to close the notice. Apart from checking the current page, you can add all kind of conditions to show notices matching different scenarios. For example, you may want to display a notice only to users with the editor user role. Editors, by default, are only able to edit pages and posts and not make changes to website settings.

To do that, you would need to implement the following piece of code in your functions.php file, replacing the aforementioned one:

function author_admin_notice(){ global $pagenow; if ( $pagenow == 'index.php' ) { $user = wp_get_current_user(); if ( in_array( 'editor', (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');

You may be able to spot that we have defined this piece of code to only apply to the user role of "Editor". This is how it will appear on your website once implemented:

WordPress custom admin notice

Custom WordPress dashboard widget

You can also add your own widget to the WordPress dashboard, similar to the ones you see there by default such as "At a Glance" and "WordPress News".

This will again require an edit to your functions.php file. You can use copy and paste the following snippet:

add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets'); function my_custom_dashboard_widgets() { global $wp_meta_boxes; wp_add_dashboard_widget('custom_help_widget', 'WIDGET TITLE GOES HERE', 'custom_dashboard_help'); } function custom_dashboard_help() { echo '<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>'; }

When implemented, the widget would look like this:

WordPress custom dashboard widget

You can modify the message to show whatever you like, using standard HTML for formatting and linking within the body area.

We hope you found these guides useful - for more website tips and advice head over to the Cosmic blog or get in touch!