Getting a warning about user_func_array in WordPress PHP...? - 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?

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);

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.

Related

Unable to pass a parameter to my function?

I have the following in a common.php file:
$debug = true;
function debug_to_screen(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
print("Debug Mode: " + $value);
}
}
}
In my main file I then call this function with the following:
require("common.php");
if(!isset($_COOKIE["qcore"]))
{
debug_to_screen("Cookie not found: ", var_dump($_COOKIE['qcore']));
}
However I'm receiving the following error:
Fatal error: Cannot pass parameter 1 by reference in
C:\DWASFiles\Sites\junglegym\VirtualDirectory0\site\wwwroot\wp-content\plugins\QCORE\login.php
on line 8
This is one of the first time's I've tried making a function that can be passed multiple values, so I don't have the skill set to understand why this isn't working. Basically - I'd like to make a debug function that I can pass multiple values to, that is defined in my common file.
Your problem is here:
debug_to_screen("Cookie not found: "...
You are passing to the function which is expecting the data to be passed by reference - meaning you are sending it the ACTUAL variable, not a copy of it.
You will need to make the array first, then pass i by reference to the function.
Something like this:
$array=$_COOKIE;
debug_to_screen($array);
In your function, you defined it as:
function debug_to_screen(&$array)
The extra & means the function is taking the ACTUAL variable, not a copy of it.
This will also not work:
function julie(&$bob)
{
// something..
}
julie(5);
This is because although the function can change the 5, it cannot be returned via the reference pass.
$var1=5;
julie($var1);
This would have worked perfectly well, as $var1 can be modified and the external code calling the function can use the changed variable.
You function function debug_to_screen(&$array) accepting only one argument and however you are sending text as first argument and cookies as second argument.
So
replace
debug_to_screen("Cookie not found: ", var_dump($_COOKIE['qcore']));
to
debug_to_screen($_COOKIE['qcore']);
There's no need to pass by reference, and you're passing two arguments when only one is defined. You're also trying to print an array, var_dump prints not returns.
I think you need to look at the PHP docs for the functions you're using, there are a number of issues here.

function to convert string to integer -- call to undefined function

I am creating a function that converts a users initials (STRING) to their userid (INT)
problem is when I call the function I get a call to undefined func error because the below declared function is no where to be found in the Source!
// connect to database -- this works, I checked it
function convertRadInitToRadID($radInits){
$sqlGetRadID="SELECT id FROM sched_roster WHERE radInitials == '".$radInits."'";
$resultGetRadID=mysql_query($sqlGetRadID);
$radID=mysql_result($resultGetRadID,0);
return $radID;
}
...I then create and array ($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter) of user initials, it works with no errors I tested it independently
$randKey=rand(0,(count($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter)-1));
$randRad=$radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter[$randKey];
$randAssignedRadID=convertRadInitToRadID($randRad); // call to undefined function error here
when I view source the function definition code (where I define the function) is nowhere to be seen in the source. Very strange. I tried placing it around different areas of the script, same error. The function definition is declared appropriately and is wrapped in .
Very strange. The function just doesn't appear. Quotations, syntax, semi-colons, etc are all spot on.
No syntax errors. Advice?
I Strongly agree with Answer #1.
In addition a usual problems occur in php if you are defining function after calling it. i.e. your calling code is before function defination then it will not run and will give an error of undefined function.
You can create a class then define this function in that class and on the time of calling you can call that function with help of $this->function(args)
I think this will resolve your problem in mean while i am trying to run your code on my machine, lets see what happen
May be your function is a method of some class. So, if it is, you should use it in another way:
MyClass::convertRadInitToRadID($radInits) // if static method
or like this
$class = new MyClass();
$class ->convertRadInitToRadID($radInits)
Trying to make sense of your question... Are you trying to call the function using JavaScript? If so, remember that JavaScript is run on the browser, and PHP is run on the server (and this is why when you "view source" you don't see the function anywhere). To send data back from JavaScript to PHP you should use AJAX.
I found the answer: I was using Jquery UI tabs.... there must be a conflict with tabs. When I run the code without tabs there is no issue.
Thanks for the '==' fix.. appreciate it. my bad
thanks for reminding me about the 80 char varname code limit

Yii clientScript Render Throws Error

I have the following in a partial view:
<?php
Yii::app()->clientScript->registerPackage('nivo-slider');
Yii::app()->clientScript->render();
However, I receive an error stating Missing argument 1 for CClientScript::render(),
What arguments does the render method require? I checked the docs but couldn't find anything definitive.
They seem pretty clear to me. You need to pass it a reference variable which will hold the value of the rendered string.
// as an example:
$output = NULL;
Yii::app()->clientScript->render($output);
// you can now do something with output!

php - creating a function, to use later in same page

I am trying to add a function named "addNotification" to my page "functions.php"
My function:
function addNoti($text,$userid)
{
mysql_query("INSERT INTO notifications (time,text,userid) VALUES('".time()."','$text','$userid')");
if(mysql_affected_rows() != 1)
return 2;
else
return 100;
}
A couple of hundreds line BELOW that^ (same page), I have my registration function "register":
function doRegister()
{
global $datesetting;
mysql_query("query to register goes here");
addNoti("You just joined us!","$userid");
}
Although, whenever I process my registration form, I get the following error:
Fatal error: Call to undefined function addNoti() in /home/domain/public_html/path/to/functions.php on line 278
(Line 278 is where the addNoti is called)
Whenever I try to move addNoti to another page, and load that page in functions.php I get no error.
Please help.
Thank you in advance.
Keep in mind that in PHP you can declare function in conditional block. It could be that in your code execution took the other branch and never got to the function definition. Check the code blocks, check if the code with the function definition gets executed (echo or var_dump are your friends)
Is it a typo? In your question you name the function addNotification and addNoti()...
Do you call doRegister() before addNoti() occurred in your script?! That is the question!
From the two functions only it is hard to say what the problem can be. Did you accidently put the function addNoti inside another function (before the closing bracket)?

netbeans not stepping through the function being called

In my php, I call a function if a certain value is true,
if( 1 == 1 ) {
my_function( variable );
}
I can step through the if statement and see what is going on, but for some reason, the step through, goes on the function call line, and it does not step through the actual function code. It just highlights the function call, then finishes off successfully.
Why is that happening? I would have thought it would show me the internal workings of the function but steping through the function being called?
"Step into" should enter the function.
Add breakpoint in the 1st line of function code you want to step through and try to debug.

Categories