Wordpress display error message in admin

One question we get a lot at our WordPress agency, WebDevStudios, is what to do when you receive the dreaded WordPress White Screen of Death. WordPress by default hides all error messages from displaying. This is for security reasons as error messages can actually help hackers penetrate your website.

So how do you display error messages in WordPress? Easy! Just add the line of code below to the top of your .htaccess file in the root directory of your WordPress website.

php_flag display_errors on

That’s it! Error messages will now display across your entire WordPress website.

Another method used to display WordPress error messages is the WP_DEBUG flag:

define(‘WP_DEBUG’, true);

Just drop that line of code in your wp-config.php file and errors will start displaying.

Remember to only use these techniques to help troubleshoot errors and remove it as soon as you are done.

Last updated on November 9th, 2018 by Debjit Saha

Wordpress display error message in admin

Do you want to display a custom error message within the theme admin page? While there’s probably a plugin for this, we have created a quick code snippet that you can use to add custom error message to admin panel in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

add_action( 'admin_notices', 'custom_error_notice' );
function custom_error_notice(){
     global $current_screen;
     if ( $current_screen->parent_base == 'themes' )
          echo '<div class="error"><p>Warning - If you modify template files this could cause problems with your website.</p></div>';
}

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 28 best WordPress resume themes to create best impression.

Wordpress display error message in admin

Posted in Marketing by Samantha Rodriguez

Last updated on February 2nd, 2022

When you’re a WordPress website owner or a plugin/theme author, admin notices offer an efficient way to communicate with users through simple alerts and messages. However, if you’ve never used them before, you might be wondering how to create custom admin notices in WordPress. 

The good news is that there are multiple solutions available. One way to create custom WordPress admin notices is by manually editing your site’s code. Alternatively, if you want a less technical method, you also have the option of using a plugin. 

In this post, we’ll discuss the benefits of adding custom admin notices in WordPress. Then we’ll walk you step-by-step through how to do it, both with and without a plugin. Let’s get started!

What Are the Benefits of Custom Admin Notices?

WordPress admin notices are useful tools that you can use to display important alerts and messages to your users. Custom admin notices enable you to:

  • Quickly and easily display useful information across your site, including admin reminders
  • Help new users navigate the WordPress admin area with ease, including for multi-author sites
  • Display success, warning, and error messages in the admin area
  • Provide feedback and guidance to users 

Custom admin notices enable you to send out dismissible notifications that can be color-coded based on the type of message. For example, you can use red for warnings and errors, green for success messages, and blue for informative notices.

How to Add a Custom Admin Notice to WordPress: No Plugin

If you’re comfortable working with code and want as much flexibility and control as possible, you can add custom admin notices in WordPress without using a plugin. Let’s take a look at how to do that in two steps.

Step 1: Add Code to the functions.php File

To manually add a custom admin notice in WordPress, locate and open the functions.php file for your WordPress theme. Then add the following code snippet:

function general_admin_notice(){
    global $pagenow;
    if ( $pagenow == 'options-general.php' ) {
         echo '<div class="notice notice-warning is-dismissible">
             <p>This is an example of a notice that appears on the settings page.</p>
         </div>';
    }
}
add_action('admin_notices', 'general_admin_notice');

This displays a notice with a yellow border on your settings page, which looks like this:

In the above code, the $pagenow variable is used to detect the page, and the following condition verifies whether the current page should display the notice. You can use notice-error, notice-warning, notice-success, or notice-info to create your custom admin notice.

If you want to configure the notice so it only displays for the ‘author’ user role, you can do so by adding the following code snippet to your functions.php file:

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

In the above code, you’ll notice the added function for detecting the user role. Now, the admin custom notice will display like this, but only for authors:

You can use these code snippets as a template for creating your own custom admin notice in WordPress, substituting or adding any conditions, hooks, and filters to meet your needs.

How to Add a Custom Admin Notice to WordPress: With a Plugin

If you’re looking for a simpler method than editing your site’s code, or you don’t need much in terms of flexibility, you can also add a custom admin notice in WordPress by using a plugin. 

Let’s take a look at how this process works.

Step 1: Download and Install the Appropriate Plugin

The first step is to download a plugin that lets you add custom admin notices in WordPress, such as KJM Admin Notices:

After you download it from the WordPress Plugin Directory, upload the plugin to your site (Plugins > Add New > Upload Plugin). Then install and activate it.

Step 2: Configure the Plugin Settings

The next step is to enable the “KJM Admin Notices” option on the plugin’s settings page. Navigate to Settings > KJM Admin Notices:

Check the boxes to activate KJM Admin Notices, and create a custom post type for adding and editing custom admin notices:

On this page, you can also select the option to email users when a notice is published, and enable comments. Select Save Changes when you’re done here. 

Step 3: Create a Custom Admin Notice

Next, hover over the Notices menu item that was added to your WordPress admin dashboard, and select Add Notice:

Similar to creating a regular post or page in WordPress, you can enter a title and description for the admin notice:

To the right, under Notice Cats, select a category for the notification:

Choose the category you want to use for this custom admin notice.

Step 4: Add User Roles That Will See This Notice

After you’ve filled in the notification’s details and selected a category, scroll down to the Show Notice To Roles section:

Select which user roles you want to see this notification. In the following section, you can further customize the notice, for example by hiding the dismiss button or changing the background and text colors:

When you’re done, click on the Publish button:

That’s it! You’ve now created a WordPress custom admin notice.

Explore WP Engine’s Developer Resources

WordPress admin notices are a useful way to display information, alerts, and messages to your users. Whether you’re a plugin or theme author, or just an individual site owner, you can create custom admin notices by manually editing your WordPress theme’s functions.php file, or by using a plugin such as KJM Admin Notices. 

However, for your WordPress admin notices to be most effective, it’s important that all aspects of your website and admin area are optimized. At WP Engine, we offer quality hosting plans that can help you deliver an exceptional digital experience to your customers!

How do I show error messages in WordPress?

Native WordPress error logging WordPress has a built-in mechanism to log errors but it's not enabled by default. To turn it on, add the WP_DEBUG constant in your wp-config file. WordPress will turn on debugging and generate a debug. log file under {your website}\wp-content\debug.

How do I add admin notices on WordPress?

How to Add a Custom Admin Notice to WordPress: No Plugin.
Step 1: Add Code to the functions. ... .
Step 2: Edit Code to Display Messages Only to Authors. ... .
Step 1: Download and Install the Appropriate Plugin. ... .
Step 2: Configure the Plugin Settings. ... .
Step 3: Create a Custom Admin Notice. ... .
Step 4: Add User Roles That Will See This Notice..

How do I change the error message in WordPress?

You can change this message yourself with WordPress Customizer. Go to Appearance > Customize from your WordPress dashboard. In the left sidebar, choose Password Protect WordPress Form > Error Message.

What WP action or filter would you use to display a message at the top of the admin panel if a certain plugin is not active?

do_action( 'admin_notices' )