2

Ok so i have added a custom field into the users edit profile page where they can add a link to a url of an image they want. I am currently using this for contribitors and above but i also have a small little profile box on my site that gets a users avatar from gravatar. I used the code from before but changed it around to get the current signed on users authorpic or get the gravatar. See code below

$profilepic = get_user_meta('author_pic');
$imgtagbeg = ('<img style="height:52px; width:52px" src="');
$imgtagend = ('"/>');
    if ($profilepic)
        echo $imgtagbeg,$profilepic,$imgtagend; 
    else
        echo get_avatar( $user_email, '52', $default = 'http://www.gravatar.com/avatar.php?gravatar_id=$md5&size=80&default=$default' );

What i want to do is if the current user signed in has a avatar linked in his profile field then use that instead of gravatar. But if the author does have a gravatar and has left the field blank in the profile use that or get the default gravatar

Update: What about something like this

$userpic = '<img src="link/to/author_pics/$userid.png"';
    if ($userpic)
        echo $userpic;
    else
        echo get_avatar( $user_email, '52', $default = 'http://www.gravatar.com/avatar.php?gravatar_id=$md5&size=80&default=$default' );

Instead of relying on a user field just upload an image to a directory and use there userid as the filename then load it else use gravatar. Would that work.

UPDATE 2:

$userpiccur = wp_get_current_user();
$userpicloc = 'http://avatars.mydomain.com/';
$userpictyp = '.png';
$userpicurl = $userpicloc . $userpiccur->user_login . $userpictyp;
$header_response = get_headers($userpicurl, 1);
$userpicbeg = '<img style="height:52px; width:52px" src="http://avatars.mydomain.com/';
$userpicend = '.png"/>';
if ( strpos( $header_response[0], "404" ) !== false )
    echo $userpicbeg,$userpiccur->user_login,$userpicend;
else
    echo get_avatar( $user_email, '52', $default = 'http://www.gravatar.com/avatar.php?gravatar_id=$md5&size=80&default=$default' );

3 Answers 3

1

From the core:

function get_user_meta($user_id, $key, $single = false) {
    return get_metadata('user', $user_id, $key, $single);
}

Pass the user id as the first argument, the key 'author_pic' as the second and TRUE as the third (to get a string value back).

2
  • Can you explain better. I am putting this in a profile template page not functions Commented May 23, 2011 at 23:40
  • nvm check out my updated question. Let me know if that'll work Commented May 23, 2011 at 23:50
1

Your code in the edit part of the question should work, and also your first part but you need to add the user ID to get_user_meta() function so use your first code but change the first line to this:

$profilepic = get_user_meta($user_id,'author_pic',true);

and make sure $user_id hold the user's actual ID.

11
  • Thanks i think i will use my edit part instead. Seems it would be better that way i can control who can add there own custom pic or not. Commented May 24, 2011 at 0:29
  • you can control better with a meta field and its much easier, but i just remembered that you will need to check if the file exists and not just if( $userpic)
    – Bainternet
    Commented May 24, 2011 at 0:34
  • do you think i should use this $user_login instead Commented May 24, 2011 at 0:35
  • Ok so if i need to check for file how do i do that Commented May 24, 2011 at 0:40
  • 1
    Ok i see what you mean, well then your code looks about right.
    – Bainternet
    Commented May 24, 2011 at 6:56
0

Ok so i got it working like i want. Now all i have to do is figure out if i want a user to be able to upload there own image or not.

<?
$userpiccur = wp_get_current_user();
$userpicloc = 'http://avatars.mydomain.com/';
$userpictyp = '.png';
$userpicurl = $userpicloc.$userpiccur->user_login.$userpictyp;
$userpicbeg = '<img class="avatar avatar-52 photo" style="height:52px; width:52px" src="';
$userpicend = '"/>';
if (@fopen($userpicurl, "r"))
    echo  $userpicbeg,$userpicurl,$userpicend;
else
    echo get_avatar( $user_email, '52', $default = 'http://www.gravatar.com/avatar.php?gravatar_id=$md5&size=80&default=$default' );
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.