最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - add_action with associative array

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论