WordPress: How to add Pinterest ‘Pin It’ button

WordPress: How to add Pinterest "Pin It" button

Pinterest is growing as a social media website and you can drive more traffic from Pinterest by allowing your readers to easily add it (or Pin It) to Pinterest. Do you want to know how to add Pinterest Pin It button to your WordPress website or blog?

Just copy the following code and paste it just above the </body> tag in the footer.php file.

<script type="text/javascript">// <![CDATA[
(function() {
    window.PinIt = window.PinIt || { loaded:false };
    if (window.PinIt.loaded) return;
    window.PinIt.loaded = true;
    function async_load(){
        var s = document.createElement("script");
        s.type = "text/javascript";
        s.async = true;
        s.src = "http://assets.pinterest.com/js/pinit.js";
        var x = document.getElementsByTagName("script")[0];
        x.parentNode.insertBefore(s, x);
    }
    if (window.attachEvent)
        window.attachEvent("onload", async_load);
    else
        window.addEventListener("load", async_load, false);
})();
// ]]></script>

Now, add the following code in single.php and/or page.php at the desired locations to show the Pinterest ‘Pin It’ button in WordPress blog posts and pages.

<?php $pinterestimage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>
<a href="http://pinterest.com/pin/create/button/?url=<?php echo urlencode(get_permalink($post->ID)); ?>&media=<?php echo $pinterestimage[0]; ?>&description=<?php the_title(); ?>" class="pin-it-button" count-layout="vertical">Pin It</a>

If you want to have a shortcode to add Pinterest Pin It button at your desired locations in WordPress blog posts, don’t make any change to single.php or page.php. Just add the following code to your theme’s functions.php file.

<?php
function get_pin($atts) {
$pinterestimage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
return '<a href="http://pinterest.com/pin/create/button/?url' . urlencode(get_permalink($post->ID)) . '&media=' . $pinterestimage[0] . '&description=' . get_the_title() .'" class="pin-it-button" count-layout="vertical">Pin It</a>'; }

add_shortcode('pinit', 'get_pin');
?>

Now, you can add a Pinterest Pin It button by adding the shortcode [pinit] in your WordPress blog posts and pages. Start pinning.

Premium WordPress Themes

Reliable Web Hosting

Smart Web Worker strongly recommends HostGator for your website/blog hosting. Sign up for web hosting at just $3.96/month. Use the coupon code "SMARTBLOGGER25" to get 25% discount on any hosting packages. Get an account with HostGator now!

4 thoughts on “WordPress: How to add Pinterest ‘Pin It’ button

  1. I am sorry to say that after following your instructions to use ashortcode to add Pinterest Pin It button to the theme’s functions.php file, I got thw “white screen of death and could not login anywhere I use the wordpress twenty eleven theme. You have caused me a massive headache. I have now managed to finally restore the site, but there is now loads of work to be done to get it looking right again. Why have you given information that DOES NOT WORK?!?

    • Hey Jane,
      Sorry to hear that the suggest code broke your theme. As I am awfully late, I can only say that the next time you have any trouble due to the codes listed on this site, please feel free to shoot an email. I promise to get back with a solution as soon as I receive the email. The same goes for all other readers.
      Regards,
      Pritam

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>