Sometime you really need to remove the administration notices injected by other themes or plugins from your theme or plugin administration pages.

If I can understand that admin notices could be very useful, injecting them indifferently in every admin page could lead to conflicts, specially if injected in other plugin/theme pages.

Who injects such notices should limit the notices to the official WordPress admin pages and to its plugin/theme pages. But usually it does not happen.

Here few line of code you can easily use to remove those notices.

Time needed: 5 minutes.

Here the steps you can follow to correctly remove other plugin/theme admin notices in your administration panels.

  1. Attach a function to the in_admin_header hook

    This event is fired just before the admin notices even are fired (admin_notices, all_admin_notices, network_admin_notices, user_admin_notices). Use a big priority value.

  2. Detect your admin pages

    You need to know when to remove the notices and limit that action only to your administration pages. Hence you need a way to determine when the user is looking at your pages, for example looking at the “page” request parameter.

  3. Remove all registered admin notice actions

    WP lets everyone to remove all attached functions to an event, so we can remove all other plugin/theme actions.

  4. Add our notices

    Once removed other notices we can add our if needed.

A sample example of code is the one below:

The code above contains an if which must be completed and which checks if the context if an admin page of our plugin/theme or not. Hence the variable $is_my_admin_page needs to be computed.

Then we remove part of the admin notices, limiting the removal to the “admin notices” (which are the generic ones but not shown, for example, in the users’ panel) and the “all admin notices” which are shown everywhere.

Last step we add our own notices if need. The anonymous function should be completed with the right code. I’ve added that last few lines of codes otherwise you risk to remove even your own notices.

How to check the admin context

To check if you’re in your admin pages context, the quickest way to to verify if the $_GET['page'] and contains your page name. It’s a good idea to have all your admin pages registered with a unique prefix (ok, the uniqueness cannot be granted but…) and check if the value starts with that prefix.

There is even the global variable $plugin_page that should be set to the same value.

Being able to know when WordPress is loading your plugin pages, is extremely useful to respect other admin pages. One of the most recurrent problem with The Newsletter Plugin is to fight with third party plugins or themes injecting tons of JavaScript in our admin pages.

It is not nice and creates a lot of troubles. In The Newsletter Plugin, for example, we always check to be in our context before injecting the JS and CSS we need.

My excuses for my bad english but it's the only way I have to communicate with you. Would you suggest a correction? Leave a comment, thank you!
Categories: WordPress

0 Comments

Leave a Reply