1

I've added the following code to my functions.php to expand the functionality of the user profile page and be able to add links to the social media of the authors.

function my_new_contactmethods( $contactmethods ) {
// Add Site
$contactmethods['mysite'] = 'My Site';
// Add Google Plus
$contactmethods['google_plus'] = 'Google Plus';
// Add Behance
$contactmethods['behance'] = 'Behance';
// Add Twitter
$contactmethods['twitter'] = 'Twitter';
//add Facebook
$contactmethods['facebook'] = 'Facebook';
return $contactmethods;
}
add_filter('user_contactmethods','my_new_contactmethods',10,1);

On the author.php I call the links like this:

<a title="Follow me on Twitter" href="<?php the_author_meta( 'twitter', $author_id ); ?>"><img src="twitter.png" alt="" /></a>
<a title="Follow me on Facebook" href="<?php the_author_meta( 'facebook', $author_id ); ?>"><img src="facebook.png" alt="" /></a>
<a title="Follow me on Behance" href="<?php the_author_meta( 'behance', $author_id ); ?>"><img src="behance.png" alt="" /></a>
<a title="Follow me on Google+" href="<?php the_author_meta( 'google_plus', $author_id ); ?>"><img src="google-plus.png" alt="" /></a>

The problem with this solution is that it shows the image and link even if the author has not filled the corresponding field.

Would it be some easy way to just echo the links that have been previously filled?

closed as off-topic by s_ha_dum, tfrommen, kaiser, gmazzap, Rarst Jan 8 '14 at 20:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions should be specific to WordPress within defined scope (merely happening in its context, such as generic PHP/JS/HTML/CSS, is insufficient). Might be better asked at Stack Overflow or other appropriate site of Stack Exchange network." – s_ha_dum, tfrommen, kaiser, gmazzap, Rarst
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Sorry for the non-pertinence guys, next time will use StackExchange for this kind of questions. Cheers! – Eric Mitjans Jan 9 '14 at 17:40
3

This is a very simple PHP issue, unrelated to WordPress. You need to check if the value is empty before echoing it. So, store them as variables first:

$twitter  = get_the_author_meta('twitter', $author_id);
$facebook = get_the_author_meta('facebook', $author_id);
//etc

Then check them before displaying:

if(!empty($twitter)) {
    echo '<a title="Follow me on Twitter" href="'.$twitter.'"><img src="twitter.png" alt="" /></a>';
}
if(!empty($facebook)) {
    echo '<a title="Follow me on Facebook" href="'.$facebook.'"><img src="facebook.png" alt="" /></a>';
}
//etc
  • Worked, but there's an error in your code, href is duplicated, first one should be alt. echo '<a href="Follow me on Twitter" href="'.$twitter.'"></a>' Many thanks GhostToast, and sorry for the non-pertinence of the post! – Eric Mitjans Jan 9 '14 at 17:39
  • Ah yes, "title" - took me a minute to see – GhostToast Jan 9 '14 at 17:44
  • Thank you so much for this helpful answer! Did me well today :) – streetfire Feb 22 '17 at 17:05

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