can I pass arguments to my function through add_action?

后端 未结 13 2159
慢半拍i
慢半拍i 2020-11-29 01:07

can I do something like that? to pass arguments to my function? I already studied add_action doc but did not figure out how to do it. What the exact syntax to pass two argum

13条回答
  •  失恋的感觉
    2020-11-29 01:10

    Instead of:

    add_action('thesis_hook_before_post','recent_post_by_author',10,'author,2')
    

    it should be:

    add_action('thesis_hook_before_post','recent_post_by_author',10,2)
    

    ...where 2 is the number of arguments and 10 is the priority in which the function will be executed. You don't list your arguments in add_action. This initially tripped me up. Your function then looks like this:

    function function_name ( $arg1, $arg2 ) { /* do stuff here */ }
    

    Both the add_action and function go in functions.php and you specify your arguments in the template file (page.php for example) with do_action like so:

    do_action( 'name-of-action', $arg1, $arg2 );
    

    Hope this helps.

提交回复
热议问题