I'm beginning with creating plugins and I don't quite understand the process of displaying error messages. e.g. I use the php readfile function to download a file from a hidden location. If something goes wrong e.g. the file isn't found, how do you display a message at that moment.

I know you need to use add_action for 'admin_notices' but I can't quite figure out where you are supposed to put the add_action calls.

As far as I can tell, you have to create a function my_download_file which tries the download and echo's a <div class="error">. Then 'somewhere' you need to call add_action('admin_notices', 'my_download file');.

This, from reading the Action Reference, then gets called when admin_notices are being printed out. But does it always get called, or does it only get called when do_action gets called?

  • Why have the function download something and not just hard-code it into the plugin?
    – Jared
    Commented Jan 25, 2012 at 19:16
  • Its just an example, if I'm writing a system I want to be able to flash notices/errors to the user, I just can't figure out how to do it
    – icc97
    Commented Jan 25, 2012 at 20:43
  • You could try AJAX calls, WP has an excellent way to do it that you can find explained here.
    – Jared
    Commented Jan 25, 2012 at 20:51
  • 1
    In a theme, code like that goes in the theme's functions.php file. In a plugin, that could essentially go anywhere as long as it's included in your main plugin file.
    – Jared
    Commented Jan 25, 2012 at 21:11
  • 1
    What you would need to do is always run the add_action, and then in the function that is called in that action, do your conditional logic.
    – Jared
    Commented Jan 25, 2012 at 21:27

1 Answer 1


What you need to do (as we discussed in comments) is run your conditional logic inside the function that that add_action calls. For example, you're adding an action to the admin notices hook.

The action will run, but the stuff inside that action's callback will only run if you let it.

add_action( 'admin_notices', 'your_custom_function' );

function your_custom_function() {

    // You need some way to set (or retrieve) the value of your $error here (if not already set)

    if( $error ) {
        // Put everything here


In a theme, this is typically found in your functions.php file or a file included in it. In a plugin, it could be in any file also as long as it is included in your main plugin file.

  • 2
    Does $error need to be a global variable? In php globals are generally frowned upon, but I can't think of any other way to handle a situation in which my conditional is dependent on another action (in my case a transition_post_status) I want to make sure an error is displayed, or is there a better way?
    – Shawn
    Commented May 28, 2014 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.