Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' );
share|improve this question

3 Answers 3

up vote 0 down vote accepted

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' );
?>
share|improve this answer

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.

share|improve this answer
    
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. –  xLRDxREVENGEx May 24 '11 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 May 24 '11 at 0:34
    
do you think i should use this $user_login instead –  xLRDxREVENGEx May 24 '11 at 0:35
    
Ok so if i need to check for file how do i do that –  xLRDxREVENGEx May 24 '11 at 0:40
1  
Ok i see what you mean, well then your code looks about right. –  Bainternet May 24 '11 at 6:56

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).

share|improve this answer
    
Can you explain better. I am putting this in a profile template page not functions –  xLRDxREVENGEx May 23 '11 at 23:40
    
nvm check out my updated question. Let me know if that'll work –  xLRDxREVENGEx May 23 '11 at 23:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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