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 wondering what can I or should I NOT hook to after_setup_theme? I'm wondering because I have been told that I shouldn't hook wp_enqueue_style to it, but I have seen other places using it to hook wp_enqueue_script here: http://justintadlock.com/archives/2010/12/30/wordpress-theme-function-files

From what I understand this is just stuff that is theme dependent, so confused on why I shouldn't use wp_enqueue_style in it?

Can I chuck stuff like this in there too?

add_action('get_header', 'enable_threaded_comments');
add_action('widgets_init', 'unregister_default_wp_widgets', 1);
remove_action('wp_head', 'rsd_link');

Thanks!

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Think of WordPress execution as a series of rungs on a ladder: WordPress climbs one rung, then the next, then the next, and so on.

Think of action hooks as the rungs themselves. Here is a (slightly abbreviated, I think) list of those actions, or "rungs" executed during a typical WordPress request.

Thus, an add_action() call is simply an instruction to execute a function on a specific rung on the ladder. As long as you make that add_action() call on a rung earlier than the run on which you want the specified function to execute, then it doesn't matter, per se which rung you use to make the add_action() call. However, there are reasons to use specific rungs to execute functions.

To take your example: a Theme's functions.php file executes at (I believe) after_setup_theme. So, you simply can't use a Theme to add an action to any rung prior to after_setup_theme, because WordPress has already executed those actions (i.e. WordPress has already climbed those "rungs" on the ladder).

If you try to execute wp_enqueue_script() in a callback hooked into an action before init, WordPress will generate a _doing_it_wrong() notice, because wp_enqueue_script() should not be executed before init, and most correctly should be executed at either wp_enqueue_scripts (front end) or admin_enqueue_scripts-{hook} (admin).

However:

Can I chuck stuff like this in there too?

add_action('get_header', 'enable_threaded_comments');
add_action('widgets_init', 'unregister_default_wp_widgets', 1);
remove_action('wp_head', 'rsd_link');

All of these are perfectly fine being called at after_setup_theme, since all of the specified actions fire later than after_setup_theme. With add_action(), you're simply telling WordPress, "queue up this callback at the specified action." With remove_action(), you're simply telling WordPress, "remove this callback from the specified action."

Edit

In re:

Great answer - thanks. I understand most of it, but a little confused why all my calls to hooks with the add_action and remove_action are ok but the call to wp_enqueue_scripts isn't? They all fire after after_setup_theme. I know you said it's because I can't execute wp_enqueue_scripts before init; though without you telling me that how would I know? For example, how do I know if there is anything else that can't be executed before something else.

Because add_action() simply queues up a callback, inside of which functions are actually executed. But functions like wp_enqueue_script() or wp_enqueue_style() are actually functions, that execute wherever you call them.

share|improve this answer
    
Great answer - thanks. I understand most of it, but a little confused why all my calls to hooks with the add_action and remove_action are ok but the call to wp_enqueue_scripts isn't? They all fire after after_setup_theme. I know you said it's because I can't execute wp_enqueue_scripts before init; though without you telling me that how would I know? For example, how do I know if there is anything else that can't be executed before something else. –  Brett Aug 19 '12 at 14:31
    
See updated answer. The difference is that add_action() doesn't execute anything, but wp_enqueue_script() does. –  Chip Bennett Aug 20 '12 at 0:47
    
Ok thanks.... :) –  Brett Aug 20 '12 at 6:53
add comment

I would recommend to check how other themes use after_setup_theme hook to get general idea, check functions.php of themes like Twenty Eleven and Underscores (_s).

after_setup_theme is mainly used to load theme functionality files, register support for various features like, post thumbnails and post formats, load translation files, etc. You can also register action and filter on theme setup, but I like keeping them near the callback functions.

wp_enqueue_script() and wp_enqueue_style() function should be hooked into appropriate action - wp_enqueue_scripts

share|improve this answer
add comment

You shouldn't hook wp_enqueue_style function calls into that because other action hooks exist that are more appropriate.

Specifically, if you need to do enqueue-ing on the front end of the site, you should hook to the "wp_enqueue_scripts" hook. If you need to enqueue something on the admin-side of the site, you should hook to the "admin_enqueue_scripts" hook.

share|improve this answer
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.