Shortcode: Add Pinterest “Pin it” button to your posts

Pinterest is a Virtual Pinboard. It lets you organize and share all the beautiful things you find on the web. People use pinboards to plan their weddings, decorate their homes, and organize their favorite recipes. Best of all, you can browse pinboards created by other people. Browsing pinboards is a fun way to discover new things and get inspiration from people who share your interests.

This post is about how you can let your visitors pin your posts to their Pinterest boards.

Add following snippet in your functions.php

Snippet

add_shortcode('pin_it', 'wpds_pin_it');
add_action('wp_head','enqueue_pinterest_script');
function wpds_pin_it( $atts, $content=null ){
    global $post;
    extract(shortcode_atts(array(
        'url' => null,
		'title'=>null,
		'img_url'=>null,
		'button_layout'=>'vertical' // vertical, horizontal, none
    ), $atts));

	if(empty($img_url)) {
		$img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
		$img_url = $img[0];
	}
	
	if(empty($title))
		$title = get_the_title();
	
	if(empty($url))
		$url = get_permalink($post->ID);

	$output = '<a href="http://pinterest.com/pin/create/button/?url=' . urlencode($url) . 
				'&media=' . $img_url . '&description=' . get_the_title() .'" '.
				'class="pin-it-button" count-layout="vertical">Pin It</a>';

	wp_enqueue_script('pinterest-widget-js');
    return $output;
}
function enqueue_pinterest_script(){
	wp_register_script('pinterest-widget-js','http://assets.pinterest.com/js/pinit.js',null,true);
}

2 Responses to Shortcode: Add Pinterest “Pin it” button to your posts

  1. I like the valuable info you provide on your articles.
    I’ll bookmark your blog and test once more here frequently. I’m relatively sure I will be
    informed lots of new stuff right right here! Good luck for
    the following!

  2. Oh my goodness! Incredible article dude!
    Thank you so much, However I am experiencing troubles with your RSS.

    I don’t know the reason why I can’t subscribe to it. Is there anybody having
    identical RSS issues? Anybody who knows the solution will you kindly respond?
    Thanx!!

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>