The theme I'm using has a hook inside header.php file to separate the header into two parts, left & right. I want to add some content into the right section but I don't understand how can I add the additional parameter when using add_action(). Code as below,

do_action( 'sinatra_header_widget_location', 'left' );
            get_template_part( 'template-parts/header/logo' );
            do_action( 'sinatra_header_widget_location', 'right' );

How can I pass 'right' parameter into add_action? my funcions.php looks like:

add_action('sinatra_header_widget_location', 'my_custom_function');
function my_custom_function() {
    echo 'test';

1 Answer 1


How can I pass 'right' parameter into add_action?

In short: You don't pass 'left' or 'right' itself to add_action(), but you can make the parameter be available in your callback by using the fourth parameter for add_action(). See examples below.

So despite you may already know this, a hook is a specific place in a block of (PHP) code where custom functions like yours (my_custom_function()) can do a certain action like displaying a custom text, or maybe perform some database inserts.

And you can make that possible by using add_action() to register your callback/function which then WordPress will call via do_action(). I.e. add_action() registers a callback for running an action on a hook, whereas do_action() calls the callback that runs the action.

And there are different ways of calling add_action() and do_action(), but the basic ones are:

  • do_action():

    // This hook has just one parameter.
    do_action( 'hook name', $param );
    // But this one has two parameters.
    do_action( 'hook name', $param, $param2 );
    // And a hook can have as many parameters as necessary.
    do_action( 'hook name', $param, $param2, $param3, ... );
  • add_action():

    // This means the callback will receive just one parameter,
    add_action( 'hook name', 'function_name' );
    // and it's equivalent to:
    add_action( 'hook name', 'function_name', 10, 1 );
    // And same goes with this; but the priority is 9 - the default is 10,
    add_action( 'hook name', 'function_name', 9 );
    // and it's equivalent to:
    add_action( 'hook name', 'function_name', 9, 1 );
    // And this one, it uses the default priority, but accepts 2 parameters.
    add_action( 'hook name', 'function_name', 10, 2 );

So in your case, these do_action() calls:

do_action( 'sinatra_header_widget_location', 'left' );
do_action( 'sinatra_header_widget_location', 'right' );

.. are identical to the first do_action() example above, which means the hook (sinatra_header_widget_location) has just one parameter, whereby the value is either left or right.

And by default, when WordPress — via do_action() — calls callbacks registered to a hook, they will always receive the first parameter passed by the hook to do_action().

So you just need to make sure that your function accepts that first parameter.

( And the 2nd, 3rd, etc. parameters if there were any and that you wanted to access them from your callback. )

So for example, you can name it $position and do a conditional to check whether the value is left or right, because the sinatra_header_widget_location hook is being called twice — one with left being the value of the first (and only) parameter, and the other being right:

function my_custom_function( $position ) {
    if ( 'right' === $position ) {
        echo 'yay, it\'s "right"';

So I hope this revised answer helps you more :), and be sure to check the documentation for add_action() and do_action() because you'll find more information there, including user-contributed notes/examples that can assist you in using the functions (the proper way):

  • Thanks it really worked. Should I also include $position inside the add_action() as a parameter?
    – Kenshinh
    Oct 16, 2020 at 4:12
  • 1
    No, you shouldn't. See the revised the answer, which I hope is more helpful. :)
    – Sally CJ
    Oct 16, 2020 at 6:27

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.