0

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?

1
  • As a note, this error never appeared before moving the site to a different theme (and before WordPress 5). Commented Dec 20, 2018 at 17:07

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I decided to run the code immediately, which fixed the issue. Your explanation makes perfect sense, but I just found it interesting that this error never appeared before on the website.
@CarlosArellano Maybe your previous theme was never actually processing the init hook, so that if you had passed a valid callback, it wouldn't have worked? Or maybe you just changed an error reporting setting and didn't realise that this warning was always there.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.