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!
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 thatadd_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 theadd_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 toafter_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 beforeinit
, WordPress will generate a_doing_it_wrong()
notice, becausewp_enqueue_script()
should not be executed beforeinit
, and most correctly should be executed at eitherwp_enqueue_scripts
(front end) oradmin_enqueue_scripts-{hook}
(admin).However:
All of these are perfectly fine being called at
after_setup_theme
, since all of the specified actions fire later thanafter_setup_theme
. Withadd_action()
, you’re simply telling WordPress, “queue up this callback at the specified action.” Withremove_action()
, you’re simply telling WordPress, “remove this callback from the specified action.”Edit
In re:
Because
add_action()
simply queues up a callback, inside of which functions are actually executed. But functions likewp_enqueue_script()
orwp_enqueue_style()
are actually functions, that execute wherever you call them.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()
andwp_enqueue_style()
function should be hooked into appropriate action –wp_enqueue_scripts
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.