Sally Gradle Sally Gradle - 6 months ago 12
PHP Question

Wordpress Function to Create Form Returns False

I have a form-generating php file in Wordpress that calls a custom function to get user data to populate the form. The function operates properly if no user is found (returns "invalid username" as it should) but does not step through the function if the user is valid. Here is the gist of it:

function print_report($username, $var2, $var3)
{
$user = get_user_by('login', $username);
if($user) {
//get all the information and create the form
}
else echo "invalid username";
return false;
}


It seems that when a valid user is found, it does not step through the function but skips right to the end. In other words, if the user is valid, it returns "false".

I hope I have explained this well. Please let me know if you need other information.

EDIT

I have done a
var_dump()
as suggested. It finds the user, but still returns false. This is the result with my test user:

string 'jimv3000' (length=8)
object(WP_User)[1610]
public 'data' =>
object(stdClass)[1634]
public 'ID' => string '1101' (length=4)
public 'user_login' => string 'jimv3000' (length=8)
public 'user_pass' => string '*THERE IS A PASSWORD HERE*' (length=34)
public 'user_nicename' => string 'jimv3000' (length=8)
public 'user_email' => string '*THERE IS AN EMAIL HERE*' (length=18)
public 'user_url' => string '' (length=0)
public 'user_registered' => string '2016-05-16 20:37:30' (length=19)
public 'user_activation_key' => string '' (length=0)
public 'user_status' => string '0' (length=1)
public 'display_name' => string 'jimv3000' (length=8)
public 'ID' => int 1101
public 'caps' =>
array (size=1)
'subscriber' => boolean true
public 'cap_key' => string 'wp_capabilities' (length=15)
public 'roles' =>
array (size=1)
0 => string 'subscriber' (length=10)
public 'allcaps' =>
array (size=3)
'read' => boolean true
'level_0' => boolean true
'subscriber' => boolean true
public 'filter' => null
false


As always, help is appreciated.

Answer

Try this

function print_report($username, $var2, $var3)
{
$user = get_user_by('login', $username);
 if($user) {
 //get all the information and create the form
 }
 else { echo "invalid username";
 return false; }
 }