0

I'm building a plugin that iterates across and parses the arguments of various WordPress hooks, and because of the flexibility needed I'm passing a function as the callable parameter in add_action:

foreach ($events as $event) {
  add_action($event, function() use($event) {
    $args = func_get_args();

    // Do something with $event and $args
  }, 100);
}

The problem I'm experiencing is that this only ever seems to retrieve the first argument passed to the action.

Take profile_update, for example. It should pass the User ID, as well as an object of old user data, but if I inspect I only get the User ID:

var_dump($args)
// array(1) { [0]=> int(3) } 

This isn't limited to that specific hook, either; I can't seem to get more than the first argument for any hook set up this way. Any suggestions are appreciated!

2
  • I don't have a solution (Christopher's might work), but the problem is that when you run add_action() only the number of arguments that you specify with the 4th argument of add_action() are passed to your function. By default this is 1. If your callback accepts 3 arguments you need to specify add_action( 'action_name', 'callback', 10, 3 );. So if you set this number to the maximum number accepted by any of the hooks you're hooked to, you might get somewhere. Commented Jun 25, 2019 at 10:09
  • Oooh, I bet that is it. Christopher's solution worked, but I imagine your suggestion is where my issue stemmed from. Thanks for the insight! Commented Jun 25, 2019 at 19:06

1 Answer 1

0

I think you are headed down the right path, but I think that func_get_args() is only going to give you the arguments passed to that function. That is how I have always used it in the past.

I took a different approach that I think might work. I found debug_backtrace() to be helpful in debugging Actions and Filters before. And the 4th element in that array it returns you is the do_action call with all the arguments that live inside of the call you are looking to make.

Here is a snippet I put together with a few actions I tested:

<?php

// Here are the Actions I tested against.
$events = array(
    'profile_personal_options',
    'profile_update',
    'show_user_profile',
    'show_user_profile'
);

foreach ($events as $event) {
  add_action($event, function($args = null) {

    $backtrace = debug_backtrace();

    // This element is going to be the do_action call
    echo '<pre>';
    print_r($backtrace[3]['function']);
    echo '</pre>';

    $action_arguments = $backtrace[3]['args'];
    // The first element of this is going to be the $event
    array_shift($action_arguments);

    // Leaving you with the rest of the parameters available to that action
    echo '<pre>';
    print_r($action_arguments);
    echo '</pre>';

  }, 100);
}

If this is not what you are looking for, or I am off...let me know and I'd be happy to take my answer down!!

1
  • This worked perfectly. Thank you! Commented Jun 25, 2019 at 19:05

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.