Michaelbox's site feed

Social Media Shortcodes

by on Aug.23, 2010, under Web Design-Development

Have you ever had the need to link to someone’s pro­file on one of the many social web­sites online? How about link to many pro­files for many peo­ple? Doesn’t it get tir­ing writ­ing out all the links over and over again? I know I got tired of it pretty quickly. One day I was read­ing about short­codes within Word­Press installs and it struck me that I could uti­lize short­codes to auto­mate the pro­file link cre­ation process. I started out with just a twit­ter short­code to see how it would go. You can see it down below.

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

Once you put this into your theme’s functions.php file, all you need to do is type out

[twitter name="THE USER ID"]

with “THE USER ID” replaced with the actual twit­ter ID, and a link to that user’s pro­file would be dis­played, as well as what site is get­ting linked to within the parentheses.

What the short­code func­tion does is take the user ID pro­vided by you in the dec­la­ra­tion, assign it to the vari­able $name, and insert $name into the link tar­get URL, the link title, and as part of the text for the link. If there is no ID pro­vided, a link to twitter.com will be cre­ated by default.

After I had this much fig­ured out, I knew the pos­si­bil­i­ties for this short­code didn’t stop there. This short­code use could be adapted for any web­site that pro­vides a user pro­file. I pro­ceeded to adapt it for: Identi.ca, Face­book, Red­dit, Digg, Design­Float, Linkedin, Stum­ble­upon, and Myspace. All that you need to know is the user ID for each site. Below is the com­bi­na­tion of each dec­la­ra­tion ready to be pasted into your functions.php file.

function twitter_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a href="http://twitter.com/'.$name.'/" title="'.$name.'s Twitter Profile">'.$name.' (Twitter)</a>';
	}
function identica_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a href="http://identi.ca/'.$name.'/" title="'.$name.'s Identi.ca Profile">'.$name.' (Identi.ca)</a>';
}
function facebook_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a href="http://www.facebook.com/'.$name.'/" title="'.$name.'s Facebook profile">'.$name.' (Facebook)</a>';
}
function reddit_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a href="http://www.reddit.com/user/'.$name.'/" title="'.$name.'s Reddit profile">'.$name.' (Reddit)</a>';
}
function digg_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a href="http://www.digg.com/users/'.$name.'/" title="'.$name.'s Digg profile">'.$name.' (Digg)</a>';
}
function designfloat_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a href="http://www.designfloat.com/user/profile/'.$name.'/" title="'.$name.'s DesignFloat profile">'.$name.' (DesignFloat)</a>';
}
function linkedin_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a href="http://www.linkedin.com/in/'.$name.'/" title="'.$name.'s Public Linkedin profile">'.$name.' (Linkedin)</a>';
}
function stumbleupon_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a href="http://www.stumbleupon.com/stumbler/'.$name.'/" title="'.$name.'s Stumbleupon profile">'.$name.' (Stumbleupon)</a>';
}
function myspace_sc($atts) {
	extract(shortcode_atts(array(
		'name'=> '',
	), $atts));
	return '<a href="http://www.myspace.com/'.$name.'/" title="'.$name.'s Myspace profile">'.$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 remem­ber, you need to pro­vide the user ID within the short­code call on either your posts or your pages, like so.

[reddit name="tw2113"]

and the result will be tw2113 (Red­dit)

The short­code dec­la­ra­tions should be eas­ily adapt­able for any web­site that pro­vides user pro­files. With a lit­tle bit of code study­ing and changes to the appro­pri­ate spots, you can cre­ate your own to add to the list for what­ever site you want.

Hope­fully this proves use­ful to many peo­ple, espe­cially any­one who links to these pro­files fre­quently in their posts or pages.

:

Comments are closed.

My Latest Tweets

MDN is Developer Powered for Web docs, demos and more.