3

Ok so i have added a custom field inside of the WordPress edit profile field where a contributor and above can add a custom image if they do not use gravatar. Now i am trying to write a if else statement and this is what i have

<?
    $hasauthorpic = (the_author_meta('author_pic'));
    if (function_exists('get_avatar')) { echo get_avatar( get_the_author_email(), '80' );}
    else {echo '?><img class="avatar avatar-80 photo"><? $hasauthorpic ?></img><? ';}
?>

What i want to try and do is if the user does have a gravatar use it unless they have specified a link to there profile pic. Or have the author_pic become higher priority even if user has gravatar.

EDIT:

<?
$authorpic = the_author_meta('author_pic');
$gravatar = get_avatar( get_the_author_email(), '80' );
    if ($authorpic); 
    elseif (function_exists('get_avatar')) 
            echo ($gravatar); 
?>

OK so i tried the code below and that did not quite work. Maybe because i am putting this into a single-whatever.php file. The above is what i have managed to get but the only problem is when it shows it shows both the avatar and author pic link so i know i still need to add the <img> tags but that will be easy later. The one thing i did read is that your cant put a true on the_author_meta so i need help.

If you can come up with a code to hook into the gravatar stuff then ill take it. In other words if you have a code that i can place in my functions.php file thatll work to i and i would prefer that. the field name is author_pic

UPDATE: This is my final write up with the provided code from below

<?php
$authorpic = get_the_author_meta('author_pic');
$imgtagbeg = ('<img style="height:80px; width:80px" src="');
$imgtagend = ('"/>');
if ($authorpic)
    echo $imgtagbeg,$authorpic,$imgtagend; 
else
    echo get_avatar( get_the_author_email(), '80' ); 
?>

4 Answers 4

2

You need to use get_the_author_meta() instead of the_author_meta()

<?php
$authorpic = get_the_author_meta('author_pic');
if ($authorpic)
    echo $authorpic; 
else
    echo get_avatar( get_the_author_email(), '80' ); 
?>
3
  • can you explain how the get_the_author_meta will show the image when that is meant for displaying none Commented May 22, 2011 at 5:47
  • 2
    echo $authorpic; will echo whatever you are storing in the author_pic author meta field.
    – aaronwaggs
    Commented May 22, 2011 at 5:55
  • I did use this but changed the code a bit. Ill post my final solution above in my question but mark your solution. Commented May 23, 2011 at 2:38
2

Your if will always return true and bypass your custom author pic.

Run the if on the custom field. If it returns true then add a filter to get_avatar that uses the custom field author pic.

if ( the_author_meta('author_pic') ) {
add_filter( 'get_avatar', 'your_custom_author_pic_function' );
}
5
  • Makes sense. Still Learning. Commented May 22, 2011 at 2:46
  • Ok so what about adding an <img> tag for the author pic where do i insert this Commented May 22, 2011 at 3:20
  • In your custom function. Take a look at this question for more details on filtering get_avatar.
    – Chris_O
    Commented May 22, 2011 at 3:31
  • ok but this is not a custom function unless it would work better that way. I simply have it in a template page Commented May 22, 2011 at 3:40
  • Ok so i updated my original question maybe you can take a look at it Commented May 22, 2011 at 4:22
2

There is a sweet new plugin I recently discovered called Simple Local Avatars that will do exactly what you ask. Check it out!

1
  • seen it. not what i want i do not want my users uploading pics. and i would prefer a hard code solution Commented May 22, 2011 at 14:38
1

If I understand correct, you don't want people to upload a custom avatar, but you do want people to be able to link to a custom avatar? (and, if not, a default avatar or gravatar kicks in) If that's the case, this article will probably suit your needs http://www.billerickson.net/wordpress-custom-avatar/

1
  • i have already got this working. as well the user will not be able to link to any image. I use my own url and the server checks for the image based of of there username Commented May 30, 2011 at 22:53

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.