Warning: file_get_contents(http://graph.facebook.com/https://olegnax.com/492-custom-share-button-facebook/): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /srv/users/serverpilot/apps/0olegnaxcom/public/wp-content/themes/olegnax/functions.php on line 446

Warning: file_get_contents(http://graph.facebook.com/https://olegnax.com/492-custom-share-button-facebook/): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden in /srv/users/serverpilot/apps/0olegnaxcom/public/wp-content/themes/olegnax/functions.php on line 446

Sometimes we need to change look of standard elements to make them fit well in site design. In this pots we'll show you how to make a custom Share button with counter for Facebook and integrate it as a shortcode for WordPress.

Getting statistic for URL via Graph API

Thanks to Facebook Graph API we can get statistic for URL. I.E.: by adding following URL in your browser:


You'll get response with amount of times this link was shared.

   "id": "http://olegnax.com",
   "shares": 1

Next we need to process the query results using PHP function.

Counter function for WordPress

Add to your functions.php, which is located at the root folder of your theme (or create it, in case it doesn't exist), following function:

function customFShare() {
	$like_results = file_get_contents('http://graph.facebook.com/'. get_permalink());
	$like_array = json_decode($like_results, true);
	$like_count =  $like_array['shares'];
	return ($like_count ) ? $like_count : "0";

At the third line we form a URL for a query using get_permalink() function, which returns URL of the current post.
Then, at the fourth line, we transform the results into array. At the line number 5, we assign shares value to variable and then, at line number 6, checking the condition of shares content and return 0 if it's empty.

WordPress shortcode

Add following lines to your functions.php for shortcode:

function customFShareShortcode( $atts, $content = null ) {
       'type' => ''
    ), $atts));
	return "<a href='http://www.facebook.com/sharer/sharer.php?u=". get_permalink() ."' rel='nofollow' class='".$type."'><strong>Share</strong> <span>". customFShare() ."</span></a>";
add_shortcode('customshare', 'customFShareShortcode');

Because we have two Facebook Share button designs in our example, we have to add type parameter for shortcode, which is then output as a class reference. This way we can make different designs for the button.

No we only need to add shortcode [customshare type=shareTxt] or [customshare type=shareCircle] at the editor and you will get the text link of Share button with counter.

Custom Share button design for Facebook

Create customshare.css and add following styles for your buttons:

a.shareCircle, a.shareTxt {
a.shareCircle {
	background:url(shareCircle.png) no-repeat 0 0;
a.shareTxt {
	background:url(shareTxt.png) no-repeat 0 0;
a.shareCircle strong, a.shareTxt strong {
a.shareCircle span, a.shareTxt span {
	font:12px/12px Arial, Helvetica, sans-serif;
	-webkit-border-radius: 5px;
	-moz-border-radius: 5px;
	border-radius: 5px;
a.shareCircle span {
	margin:12px 0 0 107px;
a.shareTxt span {
	margin:1px 0 0 98px;
a.shareCircle:hover, a.shareTxt:hover {
	background-position:0 100%
a.shareCircle:hover span {
a.shareTxt:hover span {

In our example we save customshare.css in customshare folder at the root of current theme (/wp-content/themes/yourcurrenttheme/customshare/).

Now add the styles in our theme for this, add the following code in your functions.php:

function customFShareCSS() {
		wp_enqueue_style( 'customshare',   get_template_directory_uri() . '/customshare/customshare.css','','1', 'all');

add_action('wp_print_styles', 'customFShareCSS');

This way style will be added for all front-end pages. You can add you Conditional Tags, to determine on which pages to display these styles.

Custom Facebook Share button examples:

Share 0
Share 0

You can download the sources for free from the link below and add those buttons to your WordPress by extracting file at the root of your current theme.
Note: don't forget to move all the code from functions.php to your existing functions.php.

Sources also contain layered PSD with the custom Facebook Share button from the examples.

Download for free

Don’t be shy to share this post if you found it useful.
Thank you.