I have a theme which includes some setup, using after_setup_theme
but I’d like to write my own functions which also need to run after_setup_theme
. I’d prefer to keep my stuff in a separate file. Can I call after_setup_theme
multiple times?
Leave a Reply
You must be logged in to post a comment.
WordPress hooks work like Hollywood: you don’t call them, they call you. But unlike Hollywood, they keep calling everyone on the list.
It’s normal for an action or a filter to have multiple functions hooked to it, from different plugins, or even just different functions in the WordPress core that all do something specific. It is not only possible, but even good practice, as it means your code gets clearer (each function does only one thing) and it’s easier to disable one specific piece of functionality by unhooking it.
Remember that you can also play with the priorities of hooks: if you want to run both
functionA()
andfunctionB()
in theafter_setup_theme
, butfunctionA()
must run beforefunctionB()
, you can hookfunctionA()
with the default priority10
andfunctionB()
with priority20
(or any other number above 10). What won’t work is hooking another function to an action while that action is executing. So you can’t hookfunctionB()
toafter_setup_theme
fromfunctionA()
, called onafter_setup_theme
. You could call it directly, but you would lose the benefit of separate hooks.My suggestion would be to have a “master” function, if you will, that calls all your other functions. That way you only have to hook into that action one time.
This has the added benefit of being able to return values that you can use in subsequent function calls.
Yes, you may add as many actions to the hook as you wish. Just try it.