Home / admin / Add custom error messages to admin panel
  • How to 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.

    ( click code to copy )

    wordpress snippet : PHP

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

    ( WordPress codex functions, hooks, in this snippet. )

    template, add_action, _n, _e,
  • That’s a great one, a lot of fun could be had with that :-)

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

  • Dylan Forde

    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? 

    • 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;

      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.

  • Arif

    Thanks. It really helps me…..