• Hi guys.

    I am looking for some code that would get the user data from a different user.

    The current user is logged in and has some extended profile information, including a secondary email address (non-unique). This secondary email address will be the same email address as a related user (a friend or partner)

    I can easily call up the secondary email address and echo onto the page, but I would like to know how to within a PHP snippet to use the secondary email address to then get other data from the user of that email address.

    This code gets the secondary email of the current user. I want to take the result and use it to get data from the user it belongs to.

    <?php $user_info = get_userdata(1);
             echo 'Username:  ' .$user_info->user_login . "\n";
     echo "</br>";  
          echo 'User ID: ' . $user_info->ID . "\n";
    echo "</br>"; 
     echo 'Partners Email Address: ' . $user_info->secondary_user_email . "\n";
    ?>
    
Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter Nick Bacon

    (@theburger)

    Thanks, I have looked at this before… I have tried again and got it to work on the admin account, but when switching users, it does not display changed the displayed data, it remains the same as the admin details.

    We snippets work dynamically when using a plugin to add php into a page?

    Moderator bcworkz

    (@bcworkz)

    Your code is only getting the admin’s data: get_userdata(1)
    In place of the 1, use get_current_user_id().

    Thread Starter Nick Bacon

    (@theburger)

    Thankyou for your help. That works perfectly

    Thread Starter Nick Bacon

    (@theburger)

    Another quick question.

    Now that I can get data from 2 users and display them on screen. I would like to compare

    So….
    Current user data is fetched using “user_info and the other user is fetched using “user” in my php

    if the current user is age_group = 18-29 and the other user is age_group = 18-29

    if $user_info->age_group == $user->age_group {
        echo "Your ages match";
    } else {
        echo "Your ages don't match";
    }
    

    I have no idea how to write the code.

    Thread Starter Nick Bacon

    (@theburger)

    I have figured out the above question but can not work out how to work with form data with multiple options.

    The profile will have options the user selects from, so in their profile, one question could be… DO YOU LIKE FOOTBALL. The choices are YES to play, YES to watch, No

    So how can I get a statement “YOU BOTH LIKE FOOTBALL TO WATCH”, if both users have that option set in their profile, or YOU BOTH LIKE FOOTBALL but one likes to watch and the other likes to play.

    Moderator bcworkz

    (@bcworkz)

    Specifics depend on how the data is stored. Boolean values are often 1 or 0, but can be true or false. Logical comparisons would treat either the same way. Bear in mind I’ve no idea what the user info data looks like, it’s not default user data. Anyway, you can use && AND and || or logic operators in if() conditionals like so:
    if( $userinfo->likes_football && $user->likes_football ):
    which will return true only if both users selected football as their like.

    Thread Starter Nick Bacon

    (@theburger)

    I have checked the database
    Let’s work with the following
    meta_key = age_group
    meta_value = 56-65

    for the meta_valuethere is a dropdown box with 4 different age ranges to select from.

    Obviously, this is coded completely wrong but it might show you what I am trying to do. There are many meta_keys in the profile and some have the same meta_value, so the code needs to use the key to get the value.

    if ($user_info(age_group)->56-65 && $user(age_group)->56-65);
    {echo "You both are the same age group";}
    Moderator bcworkz

    (@bcworkz)

    Custom meta values are not normally part of the WP_User object returned by get_userdata(). Unless you know otherwise for your specific situation (plugins could alter what data is available in the object), get specific meta values with get_user_meta(). Thus you could do

    if ( '56-65' == get_user_meta( $userinfo->ID, 'age_group', true )
           &&  '56-65' == get_user_meta( $user->ID, 'age_group', true )) {
        echo 'You both are the same age group';
    }
    Thread Starter Nick Bacon

    (@theburger)

    Thankyou once again, that works well.

    For you information, I am using Ultimate member plugin, and this plugin stores the user meta values to the wp_usermeta part of the database along with all the default user metadata, so it is easily looked up the same as default user metadata.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘PHP to get user data based on email address’ is closed to new replies.