Shortcode: Correct way of displaying a Facebook Share Button with or without counter

Earlier, I posted about Correct way of displaying a Twitter Share Button with or without counter. Now, almost the same thing but for Facebook. Similar to Twitter, it will not load the twitter javascript on every page of the site but only on the page where shortcode is used. It will load it at footer instead of middle or start of the contents which as a very awful approach as it effects the page load time.

You can add one of the following codes where you want to display it.

Usage



Add following snippet to “functions.php”

Snippet

add_shortcode('facebook', 'wpds_facebook');
add_action('wp_head','enqueue_facebook_script');
function wpds_facebook( $atts, $content=null ){
	 
    extract(shortcode_atts(array(
        'url' => null,
        'align' => 'left', //left, right, none
	'type' => 'box_count', //box_count, button_count, button, icon
    ), $atts));
	
    $output = "<div style='float:".$align."; margin:0 5px;' ><a '";
    if(!empty($url))
        $output .= "expr:share_url='".$via."'";
    else
        $output .= "expr:share_url='".get_permalink()."'";
	
    $output .= " href='http://www.facebook.com/sharer.php' name='fb_share' ";

    if(!empty($type))
        $output .= 'type="'.$type.'" ';

    $output .='>Share</a></div>';


    wp_enqueue_script('facebook-widget-js');
    return $output;
}
function enqueue_facebook_script(){
	wp_register_script('facebook-widget-js','http://static.ak.fbcdn.net/connect.php/js/FB.Share',null,true);
}

One Response to Shortcode: Correct way of displaying a Facebook Share Button with or without counter

  1. Randy says:

    Randy…

    It’s a shame you don’t have a donate button! I’d without a doubt donate to this fantastic blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to new updates and will share this blog with my …

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>