Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Why have the function download something and not just hard-code it into the plugin? –  Jared Jan 25 '12 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 Jan 25 '12 at 20:43
    
You could try AJAX calls, WP has an excellent way to do it that you can find explained here. –  Jared Jan 25 '12 at 20:51
    
@Jared, thanks for your comments. What I'm trying to figure out is which plugin file should add_action('admin_notices'...) go into? Should it be put in the default file that is called by wordpress or somewhere else? –  icc97 Jan 25 '12 at 21:08
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 Jan 25 '12 at 21:11
show 3 more comments

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
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 May 28 at 17:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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