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(
'header',
'branding',
'navigation',
'content',
'single',
'sidebar',
'footer'
);
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?
do_action
as a a block like this? Sincedo_action
will only run code registered withadd_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 addadd_action( '_s_before_header', function() { echo 'something'; } ) ; ?>
(or whatever) to your code.<?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 thatdo_action
will do nothing if Iam not usingadd_action
to add something. I just wanted todo_actions
directly inside the template files and do nothing else. Thanks for clarifying! (sorry edit span was over)