• Resolved starapple

    (@starapple)


    Hi,

    I’m trying to retrieve a user’s first name to insert into the body of an email. A form submits the username ($taskowner) and the user’s email address is correctly retrieved from the database and inserted into the email header. However, I can’t get the users’s ‘first_name’ using:

    $task_owner_email = $wpdb->get_var("SELECT user_email FROM$wpdb->users` WHERE ID = $taskowner LIMIT 1″);

    $task_owner = get_user_by(’email’, $task_owner_email);
    $user_firstname= $task_owner->first_name;`

    $task_owner_email, which is declared global, is correctly inserted in the email function but not $user_firstname, which is also declared global.

    Help please.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    What you’ve posted appears correct. About all you can do is do some detailed debugging to identify exactly where the values become missing. You know the email is correct. Does get_user_by() return an user object or false? Does the object contain a ‘first_name’ element? If everything checks out with getting all the values, and yet it’s gone when the email is composed, there may be a name collision with another process.

    Thread Starter starapple

    (@starapple)

    Thanks @bcworkz. The object contains a first name element because I expressly set it up to do the testing with the first_name last_name combo of Christopher Columbus.

    Do you have an example of a conditional statement I could use to determine if it is returning false?

    RGDS.

    Moderator bcworkz

    (@bcworkz)

    You could add this after the get_user_by() line:
    if ( !$task_owner ) die('No valid user found!');

    You could check the first name field with this (after the $user_firstname assignment):
    if ('Christopher' != $user_firstname ) die('No valid first name field found!');

    If execution gets past these lines, something is killing the value between this code and the email code.

    Oh, you did declare $user_firstname as global in both this function and the email code right?

    Thread Starter starapple

    (@starapple)

    Hi @bcworkz, thanks so much for your help. I decided to do a double take of the user profile and lo and behold, WP did not save first_name and last_name for that ‘user’. Nonetheless, it correctly showed ‘Display name publicly as’ Christopher Columbus. So there was some misfiring during the original entering of first and last names because a public display name is retrieved from those entries in the db. C’est la vie.

    Moderator bcworkz

    (@bcworkz)

    Glad to help. I had a feeling something odd was going on, the portion you presented is pretty straight forward and hard to get wrong. Glad you figured it out.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Can't retrieve first name with get_user_by data’ is closed to new replies.