Carlos Arellano
Carlos Arellano

Reputation: 21

Getting a warning about user_func_array in WordPress PHP...?

I am trying to pass variables from a survey onto a wordpress website via URL. While the variables do successfully pass, I also get this message:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /home4/insig14/public_html/wp-includes/class-wp-hook.php on line 286

I know that it's either an "add_action" or add_filter" issue, and the only issue I can think of is in the code I added to the functions.php :

for ($x=1;$x<=38;$x++){
    $a = "tsati";
    $b = $a . $x;
    add_action('init', add_get_val($b));

}

function add_get_val($b){
    global $wp;
    $wp-> add_query_var($b);
}

There are 38 variables I'm passing with each variable being "tsatiX" (ex. tsati1, tsati2...). The variables successfully are passed, but the error keeps appearing on top of the website. It's probably a problem with my "add_action" function, but I'm not sure what it is. Any help?

Upvotes: 0

Views: 118

Answers (2)

IMSoP
IMSoP

Reputation: 97658

Your mistake is on this line:

add_action('init', add_get_val($b));

This function is intended to register a callback to be run when WordPress is processing 'init' actions - the second argument should be the callback to run. But you are actually running the function, so passing in its result.

This might be clearer if we split the line in two with a temporary variable:

$temp = add_get_val($b);
add_action('init', $temp);

So add_get_val is being run immediately, but since it doesn't return anything, null is being passed to add_action. WordPress doesn't check this, and so later tries to execute null as a callback, giving you the error.

If you want to register add_get_val as the callback, pass it in as a string:

add_action('init', 'add_get_val');

Alternatively, you might want an anonymous function to be the callback:

add_action('init', function() use ($b) { add_get_val($b); });

Or maybe it's fine to just run that code immediately, and not register it from the init hook at all, since your code seems to be working OK, in which case you can just run:

add_get_val($b);

Upvotes: 2

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

The second parameter of add_action is supposed to be a callable, but you're passing null instead, because the add_get_val function doesn't return anything. If you meant to pass the add_get_val function instead of the result of calling that function, just pass its name as a string.

add_action('init', 'add_get_val');

I'm not sure if that is actually what you meant to do, but that's why you're getting that error anyway.

Upvotes: 1

Related Questions