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.
Thanks. It really helps me…..
Cool Arif glad I could help.
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;
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.
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.