How to get the USER avatar URL as opposed to SITE Avatar URL

Using get_avatar_url( $current_user->ID); I receive the SITE avatar URL located at gravatar.com. The same is true for get_avatar_url( get_the_author_meta ( 'ID' ));. The two URLs are different, but both are at gravatar.com.

How do I get the USER avatar URL located at mygreenboard.com? - Upon sign-up users upload their USER avatar. I need access to the USER avatar URL to copy it to another part of the mygreenboard system.

Thanks,
Rob

  • Rupok

    Hi robbiet63, thanks for asking.

    All avatars are stored in the "avatars" folder inside your domain root directory. So if you need to call the custom uploaded avatar which is stored inside your hosting, you can use the following code:

    <?php global $current_user;
    echo "http://yoursite.com/avatar/user-".$current_user->ID."-32.png"; ?>

    Note: Replace "yoursite.com" with your real domain. You can replace "32" with "96" for bigger image.

    Please let us know if you have any confusion or if you are stuck at any point. We will be glad to help further.

    Have a nice day. Cheers!
    Rupok

  • robbiet63

    Hi Rupok,

    Thank you for your suggestion. I tried it. The $current_user selected upon sign-up by the multi-site system is "0". Thus the link becomes mystery man: http://mygreenboard.com/avatar/user-0-96.png. The reason is here: http://wordpress.stackexchange.com/questions/163407/get-current-user-id-returns-0

    Here you can see the results:
    - User sub-site: http://mygreenboard.com/test5
    - $current_user workaround: http://ilove.mygreenboard.com/fordignity/sites/santropolcoffeeroasters/test5/

    Is there any way to select the sub-site user upon sign-up and get the initialized value?

    Thanks,
    Rob

  • Kasia Swiderska

    Hello Rob,

    Do I understand that you don't want to show current user avatar (current means - logged in) but avatar of the subsite admin so it will be always displayed?
    If yes then you need to use different approach. Use this function to grab subsite admin ID https://codex.wordpress.org/Function_Reference/get_users
    Like that:

    $uids = get_users(
        array(
            'blog_id'  => get_current_blog_id(),
            'role'     => 'administrator',
            'fields'   => 'ID',
            'order_by' => 'ID',
            'order'    => 'ASC',
        )
    );

    this should returns array of IDs in $uids so then

    <?php echo "http://yoursite.com/avatar/user-". $uids[0] ."-32.png"; ?>

    so that should always get you first admin of the subsite.

    If this is not what you want to achieve please provide more information.

    kind regards,
    Kasia

  • robbiet63

    Hi Kasia,

    Thank you for your help. Yes, the user avatar of the subsite admin shows in the subsite header (e.g. mygreenboard.com/test7) and in the affiliate page header (http://ilove.mygreenboard.com/fordignity/sites/santropolcoffeeroasters/test7/). To show in the affiliate page header I need to get the user ID upon sign-up, insert it into the URL, and copy the URL into the affiliate program. (I had this figured out previously in one line of code but I erased my work by mistake when updating the affiliate plugin.)

    I tried what you suggested but no-go. The affiliate program plugin script fails to complete and the user's affiliate account is not created. (https://en-ca.wordpress.org/plugins/postaffiliatepro/) I tried inserting the code you provided within the private function initAffiliate. (I already have the code for assigning the image, which is $affiliate->setPhoto(URL).) I also tried putting the call to the array in its own private function. I did not try making the $uids global. Would that do it?

    If there is any other way of getting the subsite admin user ID when logged out, please let me know.

    Thanks,
    Rob

  • Ash

    Hello Rob

    Would you please try the following code?

    $users_query = new WP_User_Query( array(
                    'role' => 'administrator',
                    'orderby' => 'display_name'
                ) );
    
    $results = $users_query->get_results();
    $uid = $results[0]->ID;

    Also, keep debug mode on so that you can track error logging.

    To enable debug mode please edit a line in your wp-config.php:
    Change

    define('WP_DEBUG', false);

    to

    define('WP_DEBUG', true);
    define('WP_DEBUG_DISPLAY', false);
    define('WP_DEBUG_LOG', true);

    This will create a file called debug.log inside wp-content folder. You can check that file. Please let us know what errors you see there.

    Have a good day!

    Cheers
    Ash

  • robbiet63

    Hi Ash,

    Thanks for your patience. I just tested as per your instructions. $uid gets set to "1" instead of the user ID for the new user. An error is reported in the debug file:

    WordPress database error Table 'WP_DATABASE_NAME.wp_586_redirection_items' doesn't exist for query SELECT wp_586_redirection_items.*,wp_586_redirection_groups.position AS group_pos FROM wp_586_redirection_items INNER JOIN wp_586_redirection_groups ON wp_586_redirection_groups.id=wp_586_redirection_items.group_id AND wp_586_redirection_groups.status='enabled' AND wp_586_redirection_groups.module_id=1 WHERE (wp_586_redirection_items.regex=1 OR wp_586_redirection_items.url='/test2?wphb-check-cf=true') made by require('wp-blog-header.php'), require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('init'), call_user_func_array, WordPress_Module->init, Red_Item::get_for_url

  • Ash

    Hello Rob

    If there is any other way of getting the subsite admin user ID when logged out, please let me know.

    As you asked, the above code should show the subsite admin user ID, not the new user ID who is registering.

    As you mentioned, also saw in a stackoverflow thread, you are getting ID 0 because the user is not created by that time. Please note that, doesn't matter if it is a multisite or not, once an user is created it will always be assigned an unique ID. That being said, before an user is created, you can never get that user ID.

    So, either that needs a page refresh to confirm that the user is logged in, and also to fetch the ID in init hook.

    About the error you mention is not related to the above code. It seems you are using a plugin for redirection that uses a table, and that table is not created for the new site yet.

    Have a good day!

    Cheers
    Ash

  • robbiet63

    Hi Ash,

    Thank you for your response and for the suggestions on how to obtain the user_id. I'm sorry if I was not clear on definitions. In this instance, the subsite admin user ID is the same as the new user ID for the user who is registering. Specifically at sign-up, when the new user's account as subsite admin is being created, and thus this user is not logged-in, I need to retrieve their user_id in order to locate their user avatar.

    I found my old code and previously get_avatar_url( get_the_author_meta ( 'ID' )); worked to retrieve the user's avatar at sign-up, before the user was logged-in. Now this no longer works. I don't know which update has prevented this from continuing to work as there have been many.

    I will continue to tinker. If you have any suggestions please let me know.

    Thanks,
    Rob