X

Add custom error messages to admin panel

Adding this snippet to the functions.php of your wordpress theme will display a custom error message displayed within the "theme" admin screen. Change the parent_base to edit, tools etc, to display message on another admin screen.



Code Snippet:


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

Comments  Leave a Reply

  1. Thanks. It really helps me…..

    1. Cool Arif glad I could help.

  2. How can you get this to display on the dashboard? I get get it to work on all the other menus such as tools, but not on the dashboard. Also, are there any other default WP classes I can use other than error? 

    1. You will notice on the line 04: $current_screen->parent_base == ‘themes’   you need to change the word themes to something else like “edit” etc. for development you can use this snippet to get the name of the current_screen.

      add_action( ‘admin_notices’, ‘current_screen’ );
      function current_screen() {
      if( !is_admin() ) return;
       
      global $current_screen;
       
      print_r($current_screen->parent_base);
      }

      add this snippet to the functions.php of your wordpress theme and within the admin it will display the name you need to replace themes with. If you want the dashboard replace themes with index. Don’t forget to remove that snippet once done since you only need to it to get the parent_base. In regards to types of messages you can change class=”error” to updated.

  3. Elliott the web design guy June 2, 2011 at 10:32 pm

    That’s a great one, a lot of fun could be had with that 🙂

    1. @elliottrichmond:disqus  yes it is something you could use just as a message to display or tie it into a plugin etc. very useful.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!