0

This is so basic, but i couldn't understand why it's not working, i'm trying to send parameter from add_action(), this is my code in my function.php:

function testingID( $testParam) {  
var_dump($testParam);
die();
}
add_action( 'init', 'testingID', 1,1); 

I expect to have the screen print '1' instead just :

string(0) ""

Wondering what caused this ?

Thanks

  • Why do you expect that value of $testParam? The last parameter of add_action is not the value of the parameter passed the callback function. – cybmeta Nov 24 '15 at 7:10
0

When an action is defined, the parameters passed to the callback are defined as well. For example, you can define an action with 4 parameters:

$a = 1;
$b = 2;
$c = 3;
$d = 4;
do_action( 'name_of_my_action', $a, $b, $c, $d );

Then, when you hook in that action, you can tell how many of those parameters will be used; the value of those params are the values assigned when the action was defined. For example, you may want to use only 2:

// The fourth parameter is the number of accepted
// parameters by the callback, in this case want only 2
// The parameters are passed to the callback in the same order
// they were set when the action was definied and with the same value
add_action( 'name_of_my_action', 'my_action_callback', 10, 2 );
function my_action_callback( $a, $b ) {

    // The value of $a is 1
    var_dump( $a );

    // The value of $b is 2
    var_dump( $b );

}

If you look at the definition of init action, eiter in docs or source code (In WodPress 4.3.1 it is in line 353 of wp-settings.php file), there is no parameters passed to the callback.

If you want to pass custom parameters to actions and filters callbacks, you have several options. I recommend this question and its answer to learn about it. For example, using anonymous functions:

$custom = 1;
add_action( 'init', function () use $custom {

    // The value of $custom is 1
    var_dump( $custom );

}, 10, 2 );
0

Where are you initializing the value of $testParam?

add_action( 'init', 'testingID', 1,1);

This specifies that testingID function should be called on init which accepts 1 parameter. But the value of parameter is not defined. Last two 1s defines, priority of function and number of parameters.

  • yes, i found out i mistaken use number of parameter as argument... – Viktor Iwan Nov 24 '15 at 17:15
-1

Here add_action( string $hook, callback $function_to_add, int $priority = 10, int $accepted_args = 1 ) hook function having four arguments:

$hook: The name of the action to which the $function_to_add is hooked.

$function_to_add: The name of the function you wish to be called.

$priority: Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.

$accepted_args: The number of arguments the function accepts. Default return 1.

I revised your code and make small change please check it.

function testingID( $testParam) {  
    // do stuff here...
    return $testParam;
}

add_action( 'init', 'testingID', 1,1);

Please review for more: codex

  • Usually, actions does not return any values; anyway I think that does not make difference in the context of the question. Also, your interpretation of $accepted_args is wrong. The number of arguments the function accepts has nothing to do with the value of the aguments and default value for $accepted_args is 1, not return 1, which are very different things. – cybmeta Nov 24 '15 at 8:58
  • @cybmeta: above detail from codex.wordpress.org/Function_Reference/add_action. So you mean that this is wrong? See Return Values (boolean) Always True. – AddWeb Solution Pvt Ltd Nov 24 '15 at 9:46
  • Returned value by an action is always true, no matter what you set as return in your callback; sorry if I didn't explain properly. I meant that implementing a return in actions callbacks is worthless and by that reason it usually not used (at less you are going to use the function callback for any other purpose). In any case, that is not what $accepted_args parameter is. – cybmeta Nov 24 '15 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.