I have been asked to do some work on a site where the old developer has written this function (create a variable that stores the users avatar into an img tag) into the functions.php:

add_filter('get_avatar', 'lb_acf_profile_avatar', 10, 5);
function lb_acf_profile_avatar($avatar, $id_or_email, $size, $default, $alt) {

    $user="";
// Get user by id or email
    if (is_numeric($id_or_email)) {
        $id = (int) $id_or_email;
        $user = get_user_by('id', $id);
    } elseif (is_object($id_or_email)) {
        if (!empty($id_or_email->user_id)) {
            $id = (int) $id_or_email->user_id;
            $user = get_user_by('id', $id);
        }
    } else {
        $user = get_user_by('email', $id_or_email);
    }
    if (!$user) {
        return $avatar;
    }
// Get the user id
    $user_id = $user->ID;
    //$user_info = get_userdata($user_id)
    // Get the file id
    $avatar_url = $user->get('user_url'); //'https://dojo.nearsoft.com/wp-content/uploads/2017/02/Eric-Wroolie-per-template.jpg';
    if ($avatar_url == '') {
        return $avatar;
    }
    $avatar="<img alt="" . $alt . '" src="' . $avatar_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';
// Return our new avatar
    return $avatar;
}

I know the function works as the app he built uses the code to generate the avatar for each user. I just don’t know how to use it and cannot reach out to him to help me utilise it.

My efforts so far have failed, using code that looks a little like this:

<?php lb_acf_profile_avatar() ?>
<?php if ($avatar != '') : ?>
    <div>Hellow World</div>
<?php endif; ?>

Where I have tried calling the function then assuming the returned variable (the avatar image) would be usable from that point. That doesn’t appear to be the case.

The error message is 5 of these one for each argument:

Warning: Missing argument 5 for lb_acf_profile_avatar(), called in /home/materialshub/public_html/development/wp-content/themes/bolt/header.php on line 238 and defined in /home/materialshub/public_html/development/wp-content/themes/bolt/functions.php on line 663 

Is there a way to tailor this so I get the avatar_url without the img tag returned, but I need the original code to function as it should as it is also used in the app and is functioning correctly.

I don’t have access to the app. Or the old developer. Any help you can provide is great. If you want further info just let me know.

I think I want a new function that gets the avatar_url like the function above but without any of the img tag. A simple url is all I need.

I need this to be dynamic as well so it works for all users automatically, generating the avatar_url. How can I pass the arguments in this manor?

I cannot just use the inbuilt get_avatar() WordPress function before we try go down that route as the app has made use of an empty field in the database ‘user_url’.

I appreciate this is quite an annoying question, but I appreciate the kindness.

EDIT: I have tried reverting back to the get_avatar() function and that then returns this warning:

Warning: Missing argument 1 for get_avatar(), called in /home/materialshub/public_html/development/wp-content/themes/bolt/header.php on line 239 and defined in /home/materialshub/public_html/development/wp-includes/pluggable.php on line 2450

Thanks, Jason.

2 Answers
2

As discussed in comments you actually just want the URL. Here’s your code modified to be a get_avatar_url hook instead:

add_filter('get_avatar_url', 'lb_acf_profile_avatar_url', 10, 5);
function lb_acf_profile_avatar_url($url, $id_or_email, $args) {
    $user="";
    // Get user by id or email
    if (is_numeric($id_or_email)) {
        $id = (int) $id_or_email;
        $user = get_user_by('id', $id);
    } elseif (is_object($id_or_email)) {
        if (!empty($id_or_email->user_id)) {
            $id = (int) $id_or_email->user_id;
            $user = get_user_by('id', $id);
        }
    } else {
        $user = get_user_by('email', $id_or_email);
    }
    if (!$user) {
        return $url;
    }

    $avatar_url = $user->get('user_url');
    if ($avatar_url == '') {
        return $url;
    }
    return $avatar_url;
}

Untested, sorry. It uses the same logic as the existing code to resolve the $id_or_email parameter into a user object: there’s probably some room for improvement here e.g. since the $id_or_email might already be the user object, and I’m a little nervous about the empty string checks but that’s what the existing (presumably working) code does.

You can then call WordPress’s get_avatar_url with a user ID and it should return user_url where available. I’d expect get_avatar to still work too with just this filter.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *