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 am trying to set an add_action function in my currently active WordPress theme functions.php file, for a defined do_action function within an activated WordPress plugin.

The add_action function in my currently active WordPress theme functions.php file doesn't work.

However, it works if I copy the add_action function from my currently active WordPress theme functions.php file into the activated WordPress plugin file right before the do_action function.

Is there a way to do this?

share|improve this question
1  
Apologies, I made a simple mistake (wrong theme active), the advice kindly provided by Michael below should work for most hook process order issues. –  Boyster Sep 10 '12 at 15:58

1 Answer 1

up vote 4 down vote accepted

Shot in the dark here, but...

It is entirely possible the plugin with the do_action definition is hooked before the theme is processed.

Find out where the do_action is defined, and find out when it is being hooked.

You might need to hook to the function that the do_action definition is hooked too, and THEN hook to that action definition.

Example:

Open the plugin file that has the do_action definition you are trying to hook to with your custom function.

Look to see if the do_action definition resides within a plugin function.

If so, look through the plugin to find an add_action() reference of that particular function name containing the do_action definition.

Note down what that hook is.

Now, you know when WordPress calls that plugin's function containing the do_action definition.

So now in your theme functions.php file, you might have something similar to the following code:

/**
 * This is the WordPress action the plugin's do_action function definition is 
 * hooked to.
 *
 * Example: This hook could be anything. I'm not saying the hook will be: "plugins_loaded" 
 * for sure, but if it was "plugins_loaded"... After WordPress loads and instantiates all 
 * of it's activated plugins, WordPress will fire the plugin's function containing the 
 * plugin's do_action definition (As long as the plugin you are trying to work with is 
 * activated). So you're getting on the same level as the plugin when it needs WordPress to 
 * execute this particular defined custom action and telling WordPress that your theme function
 * needs to be on that same level as well, before it can hook to your plugin's do_action reference.
 */
add_action('plugins_loaded', 'wpse_setup_theme');
function wpse_setup_theme(){
    /**
     * This your function that you want fired then the do_action is executed.
     *
     * Example: If the plugin file has a function named osmosis_jones() and 
     * inside osmosis_jones(), there is a do_action() reference. Note down 
     * the do_action tag name inside the osmosis_jones() function.
     */
    add_action('the_plugin_do_action_tag_name', 'wpse_display_theme_header');
}

function wpse_display_theme_header(){
    echo 'THEME HEADER HERE!';
}
share|improve this answer
1  
Hi Michael, thanx a lot, I tried 'plugins_loaded' and a few other hooks (according to codex.wordpress.org/Plugin_API/Action_Reference 'after_setup_theme' is the earliest hook themes can use) but no joy! :( really confused –  Boyster Sep 10 '12 at 15:46

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.