Iam working on a theme in the moment and got to the point where I also want to add some custom hooks to some locations, to enable some more flexibility.

I want to add multiple hooks to the theme. I have different sections (header, content, sidebar, footer etc) and on all those locations I want to add an before, inside and after hook.

So at first I was using a code block like this to create multiple hooks for my theme:

/* Teaser hooks */
function _s_before_teaser() { do_action('_s_before_teaser', 'teaser'); }
function _s_inside_teaser() { do_action('_s_inside_teaser', 'teaser'); }
function _s_after_teaser() { do_action('_s_after_teaser', 'teaser'); }

/* Header hooks */
function _s_before_header() { do_action('_s_before_header', 'header'); }
function _s_inside_header() { do_action('_s_inside_header', 'header'); }
function _s_after_header() { do_action('_s_after_header','header'); }


/* Sidebar hooks */
function _s_before_sidebar() { do_action('_s_before_sidebar', 'sidebar'); }
function _s_inside_sidebar() { do_action('_s_inside_sidebar', 'sidebar'); }
function _s_after_sidebar() { do_action('_s_after_sidebar', 'sidebar'); }

/* Footer hooks */
function _s_before_footer() { do_action('_s_before_footer', 'footer'); }
function _s_inside_footer() { do_action('_s_inside_footer', 'footer'); }
function _s_after_footer() { do_action('_s_after_footer', 'footer'); }

With this code I can use <?php _s_before_header(); ?> in a template file.

With this setup everything works as it should.

But I find the code, or at least the big code block ugly and also dont want to use duplicate(kinda) code.

So instead of that code block I rewrote this code to the following:

function _s_create_all_hooks() {
    $sections = array(

    foreach ($sections as $section) {
        do_action('_s_before_'.$section, $section);
        do_action('_s_inside_'.$section, $section);
        do_action('_s_after_'.$section, $section);
add_action('wp_loaded', '_s_create_all_hooks', 10, 1 );

This is also working, but...

With this method I cant use <?php _s_before_header(); ?> anymore, cause I dont have any functions anymore.

Instead I now must use this in templates <?php do_action('_s_before_header', 'header'); ?>

But Iam not to thrilled with this method and Iam also finding the do_action snippets in my template ugly.

I already searched the web, found several hook-tutorials, but in all tutorials and articles only one example hook is created. There was no article covering the creation of multiple hooks at once.

My question is, what is the best practice to create multiple hooks at once?

  • 2
    Is there a technical reason for you to add do_action as a a block like this? Since do_action will only run code registered with add_action, by default it won't do anything, so you can add it straight to your markup. This serves serves the dual purpose of documenting the theme for other developers. For example: you can add the code <?php do_action( '_s_before_header' ); ?> before the header and it won't do anything... until you add add_action( '_s_before_header', function() { echo 'something'; } ) ; ?> (or whatever) to your code. Commented Jan 18, 2017 at 12:56
  • Hi Pedro, I was using the code block so that I can write just <?php _s_before_header(); ?> in the template files where I want the hooks. I have some other functions in place to already add some content to some hooks, so not every hook will be empty. I know that do_action will do nothing if Iam not using add_action to add something. I just wanted to
    – LWS-Mo
    Commented Jan 18, 2017 at 13:10
  • ... see if there is another method. So you are saying I should/can just place the do_actions directly inside the template files and do nothing else. Thanks for clarifying! (sorry edit span was over)
    – LWS-Mo
    Commented Jan 18, 2017 at 13:20

1 Answer 1


What you did, is do_action with a single parameter.

File: wp-includes/plugin.php
417:  * @param string $tag     The name of the action to be executed.
418:  * @param mixed  $arg,... Optional. Additional arguments which are passed on to the
419:  *                        functions hooked to the action. Default empty.
420:  */
421: function do_action($tag, $arg = '') {

Since PHP is a liberal language you can also add multiple parameters to the do_action and later use the number of parameters in te add_action 4-th argument.

function _hookgenerator( $name ) { do_action($name); }

Feel free to test since this is how you can make it work. In this example, I haven't used hook arguments at all. Maybe after some thinking, you will not need that also.

  • Thanks for your answer and your snippet prosti. Before I could not wrap my head around the way you wrote this, thanks. After the answer @pedro provided I think I had just the wrong idea how to setup hooks (or not having to setup something at all). Regarding the do_action parameters: Iam using the 1 parameter that I added to do_action to generate a matching id for some of the HTML Elements: function _s_theme_section_start( $section ) { echo '<div id="full-width-'.$section.'" class="full-width">'; }; add_action('_s_before_header', '_s_theme_section_start', 10, 1 );
    – LWS-Mo
    Commented Jan 18, 2017 at 15:02
  • In WPSO @LWS-Mo, we say thanks using upvotes, and downvotes. :)
    – prosti
    Commented Jan 18, 2017 at 15:06
  • I upvoted Pedro´s comment (cause he brought me to the right thinking), upvoted your comment and marked it also as an answer. This is OK right?
    – LWS-Mo
    Commented Jan 20, 2017 at 12:11
  • Shure thing @LWS-Mo, good management.
    – prosti
    Commented Jan 20, 2017 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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