Author bio Social Links

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

Read More
   <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…

Related posts

Leave a Reply

3 comments

  1. 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 } ?>
    
  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();
    }
    
  3. The first script: getSocialLinksByAuthor returns an array of social links based on the array: $socialMetas.

    The second script: getHtmlSocialLinksByAuthor returns an html with an html list for displaying the social links based on the first script.

    In short, having both scripts in your theme’s functions.php file, but calling the second script where you want the social links: getHtmlSocialLinksByAuthor.

    /**
     * Retorna um array de Social Link do Author
     * 
     * @return array Social Links do Author
     */
    function getSocialLinksByAuthor(){
        $socialLinks = array();
        $socialMetas = array('linkedin', 'facebook', 'instagram', 'youtube' ,'twitter', 'myspace', 'pinterest','soundcloud', 'tumblr', 'wikipedia' );
        foreach ($socialMetas as $value){
            if(strlen(get_the_author_meta($value)) >5):
                $socialLinks[$value] = get_the_author_meta($value);
            endif;
        }
        return $socialLinks;
    }
    
    /**
     * Retorna um HTML de lista de Social Links para inserir em uma tag UL
     * 
     * @return string HTML de lista LI de Social Links
     */
    function getHtmlSocialLinksByAuthor(){
        $html = '';
        $socialLinksByAuthor = getSocialLinksByAuthor();
        foreach ($socialLinksByAuthor as $key => $value) {
            $fa_class = $key == 'youtube' ? 'fa-' . $key . '-play' : 'fa-' . $key;
            $html .= "<li><a href=' $value' class='social-link $key'>";
            $html .= "  <a href=' $value' class='social-link $key' title='$key do autor' target='_blank'>";
            $html .= "      <i class='fa $fa_class'></i>";
            $html .= "  </a>";
            $html .= "</li>";
        }
        return $html;
    }