Social Media Shortcodes

⏲️ Time: 2 mins

Have you ever had the need to link to someone’s profile on one of the many social websites online? How about link to many profiles for many people? Doesn’t it get tiring writing out all the links over and over again? I know I got tired of it pretty quickly. One day I was reading about shortcodes within WordPress installs and it struck me that I could utilize shortcodes to automate the profile link creation process. I started out with just a twitter shortcode to see how it would go. You can see it down below.

function twitter_sc($atts) {
	extract(shortcode_atts(array(
		'name' => '',
	), $atts));
	return '<a title="'.$name.'s Twitter Profile" href="http://twitter.com/'.$name.'/">'.$name.' (Twitter)</a>';
	}
add_shortcode('twitter', 'twitter_sc');

with “THE USER ID” replaced with the actual twitter ID, and a link to that user’s profile would be displayed, as well as what site is getting linked to within the parentheses.

What the shortcode function does is take the user ID provided by you in the declaration, assign it to the variable $name, and insert $name into the link target URL, the link title, and as part of the text for the link. If there is no ID provided, a link to twitter.com will be created by default.

After I had this much figured out, I knew the possibilities for this shortcode didn’t stop there. This shortcode use could be adapted for any website that provides a user profile. I proceeded to adapt it for: Identi.ca, Facebook, Reddit, Digg, DesignFloat, Linkedin, Stumbleupon, and Myspace. All that you need to know is the user ID for each site. Below is the combination of each declaration ready to be pasted into your functions.php file.

function twitter_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a title="'.$name.'s Twitter Profile" href="http://twitter.com/'.$name.'/">'.$name.' (Twitter)</a>';
	}
function identica_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a title="'.$name.'s Identi.ca Profile" href="http://identi.ca/'.$name.'/">'.$name.' (Identi.ca)</a>';
}
function facebook_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a title="'.$name.'s Facebook profile" href="http://www.facebook.com/'.$name.'/">'.$name.' (Facebook)</a>';
}
function reddit_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a title="'.$name.'s Reddit profile" href="http://www.reddit.com/user/'.$name.'/">'.$name.' (Reddit)</a>';
}
function digg_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a title="'.$name.'s Digg profile" href="http://www.digg.com/users/'.$name.'/">'.$name.' (Digg)</a>';
}
function designfloat_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a title="'.$name.'s DesignFloat profile" href="http://www.designfloat.com/user/profile/'.$name.'/">'.$name.' (DesignFloat)</a>';
}
function linkedin_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a title="'.$name.'s Public Linkedin profile" href="http://www.linkedin.com/in/'.$name.'/">'.$name.' (Linkedin)</a>';
}
function stumbleupon_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a title="'.$name.'s Stumbleupon profile" href="http://www.stumbleupon.com/stumbler/'.$name.'/">'.$name.' (Stumbleupon)</a>';
}
function myspace_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a title="'.$name.'s Myspace profile" href="http://www.myspace.com/'.$name.'/">'.$name.' (Myspace)</a>';
}

add_shortcode('twitter', 'twitter_sc');
add_shortcode('identica', 'identica_sc');
add_shortcode('facebook', 'facebook_sc');
add_shortcode('reddit', 'reddit_sc');
add_shortcode('digg', 'digg_sc');
add_shortcode('designfloat', 'designfloat_sc');
add_shortcode('linkedin', 'linkedin_sc');
add_shortcode('stumbleupon', 'stumbleupon_sc');
add_shortcode('myspace', 'myspace_sc');

Just remember, you need to provide the user ID within the shortcode call on either your posts or your pages, like so.

tw2113 (Reddit)

and the result will be tw2113 (Reddit)

The shortcode declarations should be easily adaptable for any website that provides user profiles. With a little bit of code studying and changes to the appropriate spots, you can create your own to add to the list for whatever site you want.

Hopefully this proves useful to many people, especially anyone who links to these profiles frequently in their posts or pages.


Michael is a seasoned developer who loves helping build stuff for the internet. He brings over a decade of varied experiences working with both front and back end developer stacks.

His primary focus has been WordPress and PHP and all the components that go along with them. During the day, he is a Support Engineer with WebDevStudios, helping clients get the best that they can out of their own websites.

Categories: Web DevelopmentTags:

Leave a Reply

Your email address will not be published. Required fields are marked *