Hashtag Your Social Share Links In WordPress

Almost every major social network use hashtags to tag and categorize the whole content in conjunction with the user and other content. In the case of spreading own content through social networks, it can be helpful to hash some of the important words.

In the following and straightforward solution, I will use the post tags to hash the words and phrases in the post title/content. This can be done with a few lines of code (It’s up to you to extend that solution on a better and more professional way!).

function get_the_social_link( $network, $hashtagged = false )
{
    global $post;

    if( $hashtagged )
    {
        $title = preg_replace( '/('.get_post_tags( $post->ID, 'posts', '|' ).')/i', '#${1}', get_the_title() );
    }else{
        $title = get_the_title();
    }

    switch( $network )
    {
        case 'twitter':
            $username = 'YOUR_TWITTER_USERNAME_HERE';
            return '//twitter.com/intent/tweet?url=none&via='.$username.'&text='.urlencode( $title ).': '.esc_url( get_the_permalink() ).'&original_referer='.esc_url( get_the_permalink() );
        break;
        case 'facebook':
            return '//www.facebook.com/sharer/sharer.php?u='.esc_url( get_the_permalink() );
        break;
        case 'gplus':
            return '//plus.google.com/share?url='.esc_url( get_the_permalink() );
        break;
        case 'xing':
            return '//www.xing.com/app/user?op=share;url='.esc_url( get_the_permalink() ).';title='.$title.';provider='.esc_url(get_bloginfo('name'));
        break;
        case 'linkedin':
            return '//www.linkedin.com/shareArticle?mini=true&url='.esc_url( get_the_permalink() ).'&title='.urlencode( get_the_title() ).'&summary=&source='.esc_url(get_bloginfo('name'));
        break;
        case 'pinterest':
            $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' );
            return '//www.pinterest.com/pin/create/button/?url='.esc_url( get_the_permalink() ).'&description='.urlencode( $title ).'&media='.$image[0];
        break;
        default:
            return;
        break;
    }
}

Store the function in the functions.php of your theme folder and call the generated link from get_the_social_link(); in a popup or modal.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn