filters - add_action with associative array

I am trying to get these parameters from the do_action placed inside body:do_action( 'custom_action',array(

I am trying to get these parameters from the do_action placed inside body:

do_action( 'custom_action',  array( 'product_id' => $product_id ,  'outbiddeduser_id' => $outbiddeduser, 'log_id' => $log_id ) );

I am trying to do it like this:

    add_action('custom_action', 'test', 10, 3);
    function test($product_id, $outbiddeduser_id, $log_id) {
         $a = $product_id;
         $b = $outbiddeduser_id;
         $c = $log_id; 

     echo $a . ', ' . $b . ', ' . $c;
    }

And this:

add_action('custom_action', 'test', 10, 1);
function test( $associative_array ) {
   $a = $associative_array['product_id'];
   $b = $associative_array['outbiddeduser_id'];
   $c = $associative_array['log_id'];

   echo $a . ', ' . $b . ', ' . $c;
}

And it's not working. What am I doing wrong?

I am trying to get these parameters from the do_action placed inside body:

do_action( 'custom_action',  array( 'product_id' => $product_id ,  'outbiddeduser_id' => $outbiddeduser, 'log_id' => $log_id ) );

I am trying to do it like this:

    add_action('custom_action', 'test', 10, 3);
    function test($product_id, $outbiddeduser_id, $log_id) {
         $a = $product_id;
         $b = $outbiddeduser_id;
         $c = $log_id; 

     echo $a . ', ' . $b . ', ' . $c;
    }

And this:

add_action('custom_action', 'test', 10, 1);
function test( $associative_array ) {
   $a = $associative_array['product_id'];
   $b = $associative_array['outbiddeduser_id'];
   $c = $associative_array['log_id'];

   echo $a . ', ' . $b . ', ' . $c;
}

And it's not working. What am I doing wrong?

Share Improve this question edited Sep 3, 2019 at 19:31 Tahi Reu asked Sep 3, 2019 at 17:15 Tahi ReuTahi Reu 3081 silver badge14 bronze badges 3
  • How are you testing this? The code examples as is don't have any means of checking if it worked or not – Tom J Nowell Commented Sep 3, 2019 at 17:40
  • Updated it again. – Tahi Reu Commented Sep 3, 2019 at 19:31
  • I see, if you don't get what you expected, what do you get instead? And can you update your code so it has values? There are variables, but it's unclear what their values are, which means it might work but it's getting empty values – Tom J Nowell Commented Sep 4, 2019 at 13:54
Add a comment  | 

1 Answer 1

Reset to default 0

You passed the action an associative array, so your hooked function will recieve an associative array. It's a little clearer if we retype it like this:

$associative_array = array(
    'product_id' => $product_id ,
    'outbiddeduser_id' => $outbiddeduser,
    'log_id' => $log_id
);
do_action( 'woocommerce_simple_auctions_outbid',  $associative_array );

Thus:

add_action('woocommerce_simple_auctions_outbid', 'test', 10, 1);
function test( $associative_array ) {

It comes in as an array, because that's what you passed through. There's no magic unpacking of the array in do_action

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745188740a4615761.html

相关推荐

  • filters - add_action with associative array

    I am trying to get these parameters from the do_action placed inside body:do_action( 'custom_action',array(

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信