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.

I'm using simple code to show user social link in author bio. in functions.php

<?php
function add_remove_contactmethods( $contactmethods ) {
    // Add Twitter
    $contactmethods['twitter'] = 'Twitter';
    //Add Facebook
    $contactmethods['facebook'] = 'Facebook';
 // Remove Contact Methods
    unset($contactmethods['aim']);
    unset($contactmethods['yim']);
    unset($contactmethods['jabber']);

    return $contactmethods;
}
add_filter('user_contactmethods','add_remove_contactmethods',10,1);
?>

in single.php

   <a href="<?php the_author_meta('twitter'); ?>" title="Twitter" target="_blank" id="twitter"><img src="/images/twitter.png" alt="Twitter" /></a>

so how can i hide social link in author bio when the field is empty in user profile.

please help me...

share|improve this question

2 Answers 2

Here is a sophisticated way of creating the social links and then retrieving them back where you need using a simple function. You can use this function to create a shortcode too. (I copied the code from one of my project, so you might want to change some of the class prefixes if you like).

    /*-----------------------------------------------------------*/
    /*   Add User Social Links (functions.php)
    /*-----------------------------------------------------------*/
    function cfw_add_user_social_links( $user_contact ) {

    /* Add user contact methods */
    $user_contact['twitter']   = __('Twitter Link', 'textdomain');
    $user_contact['facebook']  = __('Facebook Link', 'textdomain');
    $user_contact['linkedin']  = __('LinkedIn Link', 'textdomain');
    $user_contact['github']    = __('Github Link', 'textdomain');
    $user_contact['instagram'] = __('Instagram Link', 'textdomain');
    $user_contact['dribbble']  = __('Dribbble Link', 'textdomain');
    $user_contact['behance']   = __('Behance Link', 'textdomain');
    $user_contact['skype']     = __('Skype Link', 'textdomain');

    return $user_contact;
}
add_filter('user_contactmethods', 'cfw_add_user_social_links');

function cfw_get_user_social_links() {
    $return  = '<ul class="list-inline">';
    if(!empty(get_the_author_meta('twitter'))) {
        $return .= '<li><a href="'.get_the_author_meta('twitter').'" title="Twitter" target="_blank" id="twitter"><i class="cfw-icon-twitter"></i></a></li>';
    }
    if(!empty(get_the_author_meta('facebook'))) {
        $return .= '<li><a href="'.get_the_author_meta('facebook').'" title="Facebook" target="_blank" id="facebook"><i class="cfw-icon-facebook"></i></a></li>';
    }
    if(!empty(get_the_author_meta('linkedin'))) {
        $return .= '<li><a href="'.get_the_author_meta('linkedin').'" title="LinkedIn" target="_blank" id="linkedin"><i class="cfw-icon-linkedin"></i></a></li>';
    }
    if(!empty(get_the_author_meta('github'))) {
        $return .= '<li><a href="'.get_the_author_meta('github').'" title="Github" target="_blank" id="github"><i class="cfw-icon-github"></i></a></li>';
    }
    if(!empty(get_the_author_meta('instagram'))) {
        $return .= '<li><a href="'.get_the_author_meta('instagram').'" title="Instagram" target="_blank" id="instagram"><i class="cfw-icon-instagram"></i></a></li>';
    }
    if(!empty(get_the_author_meta('dribbble'))) {
        $return .= '<li><a href="'.get_the_author_meta('dribbble').'" title="Dribbble" target="_blank" id="dribbble"><i class="cfw-icon-dribbble"></i></a></li>';
    }
    if(!empty(get_the_author_meta('behance'))) {
        $return .= '<li><a href="'.get_the_author_meta('behance').'" title="Behance" target="_blank" id="behance"><i class="cfw-icon-behance"></i></a></li>';
    }
    if(!empty(get_the_author_meta('skype'))) {
        $return .= '<li><a href="'.get_the_author_meta('skype').'" title="Skype" target="_blank" id="skype"><i class="cfw-icon-skype"></i></a></li>';
    }
    $return .= '</ul>';

    return $return;
}

Now all you need to do is to do is to put this function where you want (content-single.php maybe)

<div class="author-social-links">
    <?php echo cfw_get_user_social_links(); ?>
</div>

Of course you can extend the use of conditional sentences to hide the whole when there is no social links provided for any of the fields.

Here is how you can create a shortcode from the code above:

/*-----------------------------------------------------*/
/*  Author Social Links Shortcode (functions.php)
/*-----------------------------------------------------*/

add_shortcode( 'author-social-links', 'cfw_author_social_links_shortcode' );
/**
 * this the shortcode [author-social-links]
 */
function cfw_author_social_links_shortcode() {
    return cfw_get_user_social_links();
}
share|improve this answer

You need to check, if the field is empty or not before printing link using the get_the_author_meta function.

<?php if(!empty(get_the_author_meta('twitter'))) { ?>
   <a href="<?php the_author_meta('twitter'); ?>" title="Twitter" target="_blank" id="twitter"><img src="/images/twitter.png" alt="Twitter" /></a>
<?php } ?>

or, try

<?php if(!empty(get_user_meta(get_the_author_meta('ID'),'twitter'))) { ?>
   <a href="<?php the_author_meta('twitter'); ?>" title="Twitter" target="_blank" id="twitter"><img src="/images/twitter.png" alt="Twitter" /></a>
<?php } ?>

but, for some reasons, followed code fixed it

<?php if(strlen(get_the_author_meta('twitter')) >5) { ?>
   <a href="<?php the_author_meta('twitter'); ?>" title="Twitter" target="_blank" id="twitter"><img src="/images/twitter.png" alt="Twitter" /></a>
<?php } ?>
share|improve this answer
    
Thank you for your answer but they still not working, the field is empty. :( –  Maan Dec 13 '12 at 14:06
    
1) Please make sure it does not even have a "space". 2) check output of "var_dump(get_the_author_meta('twitter'))" to see what it has that makes the if condition true. –  M-R Dec 13 '12 at 14:10
    
okay here is the update: the field is empty, when i use that code "var_dump(get_the_author_meta('twitter'))" i have got the error string(0) "" –  Maan Dec 13 '12 at 14:20
    
Can someone please give me the right code... –  Maan Dec 13 '12 at 19:29
1  
Thanks M-R That code (strlen(get_the_author_meta('twitter')) >5) are now work perfectly . –  Maan Dec 13 '12 at 19:55

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.