1

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.

Sally Gradle
  • 13
  • 1
  • 3
  • What do you have in `$username` and `$user` ? Can you do a `var_dump($username, $user)` and show us your result? – Felippe Duarte May 17 '16 at 18:20
  • unless you are telling the code to "return" something inside the if statement, you are always going to return false because the return statement falls outside of your if statement. – Lucha Laura Hardie May 17 '16 at 18:20
  • It does find the user and the data. The return is too long to post here, but it finds the user data and still returns "false" at the end. – Sally Gradle May 18 '16 at 01:37
  • Update: after looking at the var_dump I had an insight. Thanks, @Felippe Duarte – Sally Gradle May 18 '16 at 02:30

2 Answers2

3

See PHP conditionals, brackets needed?

You have no brackets around your else statement, which means that when your function enters the if statement when a user exists, it does whatever you have told it to do, and then falls out the bottom and executes your return false statement.

If you want this to return false ONLY when a user is not found, you need to put curly braces around your else statement.

You may wish to add them regardless to make your code more legible. It makes it easier to find this kind of issue.

Community
  • 1
  • 1
Lucha Laura Hardie
  • 429
  • 1
  • 3
  • 10
0

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; }
 }
Deepti chipdey
  • 1,157
  • 9
  • 19