Simple Facebook Sharing and Like button for WordPress

You do not need to use a plugin to let your readers share and like your posts and pages in WordPress.

Facebook provide code at single URL level, but you can simply add the WordPress php to call the current URL and that works a treat (get the Facebook code here).

There are two pieces of code. The first needs to be added in the header and the second where you want it to appear.

Pop this somewhere in the HEAD section:

<div id=”fb-root”></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); = id;
js.src = “//”;
fjs.parentNode.insertBefore(js, fjs);
}(document, ‘script’, ‘facebook-jssdk’));</script>

Add this to where you want the like / share buttons to appear:

<?php if(is_single()) : ?>
<div class=”fb-like” data-href=”<?php the_permalink(); ?>” data-layout=”standard” data-action=”recommend” data-show-faces=”true” data-share=”true”></div>
<?php endif; ?>

I added the php if single so that the buttons only appear on the Posts pages. Keeps the homepage and category pages tidier and there is no real reason to encourage people to share information pages.

If the WordPress editor breaks the code, I apologise now!


Leave a Reply

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