On one of my templates I have an action hook ‘do_xyz’
I want to attach a custom sidebar to the action hook do_xyz
Here is what I am trying in my theme functions.php file, does not work:
add_action('do_xyz', 'get_sidebar('secondary')' );
What is the proper way of doing this?
I know my hooks are setup right because if I attach a custom built function there, then it works. I just can’t seem to attach the sidebar the way I am trying to.
EDIT
I have this in my functions file:
function do_xzy() {
do_action(‘do_xyz’);
}
Then in my template itself I have:
do_action( 'do_xyz' );
I fully understand registering an action hook in my theme, and also adding a place for the action to occur.
I even somewhat understand add_action, it is just not liking it when I put the get_sidebar(‘secondary’) in there as my function I want to attach to my action hook.
Hope that makes more sense.
Explanation
There’s the global array
$wp_filters
. This array stores some sub arrays. One for eachpriority
. Each sub array then contains an array for each callback added. And the keys for those sub-sub arrays are the attached functions.Example
If you do the following, somewhere in your code (preferable after the
init
hook):…then you’ll see the following spit out (reduced for the answer):
Use case
So if you’re adding the following call to some template:
…then every callback function, that you attach to this hook, will be saved inside the global array.
So this means that we then have the following output from the
var_dump
(of the'hook_name'
):What happens?
So if you add a
string
viaadd_action
to some hook, then you’re basically adding data to aglobal
array.The
do_action( 'hook_name' );
then just searches for a function in the global namespace (in this case) and executes the function.The problem
You’re trying to do a lot of things without actually knowing the basics of PHP. Some notes here, but the rest will be on you and php.net:
This will drop a failure, as you’re ending the string inside the callback after the second
'
, sosecondary
stands for itself and will maybe considered a constant or just break, as you haven’t used a.
or,
to actually connect things.How to do it
If you want to add sidebars (or any other element) to a hook, then simply add a callback function that wraps your template tag
Advanced
Hooks can take arguments, so you might use a default too:
Then you can use or modify the default with conditionals for example
The example with a conditional:
Even more Advanced
Hooks can take more than one argument:
The example with two arguments:
You notice, that we now got two more values for the
add_action
call: The first is thepriority
(defaults to 10) when to execute the function inside the hook and the second is the number of arguments. Those are only needed if the arguments number is greater than one.Hope that helps clearing things out.
get_sidebar( 'secondary' );
will load the template filesidebar-secondary.php
.What you’re looking for is the
dynamic_sidebar()
function. You’ll have to also register your sidebar first usingregister_sidebar()
.So you have two options.
1) Just put the sidebar in the template file. Put it directly above or below the
do_action()
call, whichever makes sense:2) Use the hook.
Sorry to post this as an answer when it should be a comment.
After struggling for a while with calling a sidebar in a hook problem, I’d like to mention that sidebar ID should be all lower case.
I you use a capital letter in sidebar ID (ex: ‘Secondary’), the registered sidebar will appear in wp-admin, you may add widgets to it, but if you don’t register the sidebar the associated widgets will not appear as ‘dis-activated widgets’… they just disappear :-/
is_active_sidebar( ‘Secondary’ ) will always return false
dynamic_sidebar( ‘Secondary’ ) or get_sidebar( ‘Secondary’ ) will return nothing.
I’m using wordpress 4.5.2
Hope this can help someone 🙂
Have a nice day