can I pass arguments to my function through add_action?

后端 未结 13 2125
慢半拍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:34

    Well, this is old, but it has no accepted answer. Reviving so that Google searchers have some hope.

    If you have an existing add_action call that doesn't accept arguments like this:

    function my_function() {
      echo 100;
    }
    
    add_action('wp_footer', 'my_function');
    

    You can pass an argument to that function by using an anonymous function as the callback like this:

    function my_function($number) {
      echo $number;
    }
    
    $number = 101;
    add_action('wp_footer', function() { global $number; my_function($number); });
    

    Depending on your use case, you might need to use different forms of callback, possibly even using properly declared functions, as sometimes you may encounter trouble with scope.

    0 讨论(0)
提交回复
热议问题