How To Add a Pinterest Button In WordPress Without Using A Plugin

Today, you’ll learn how to add a Pinterest button to your WordPress site without using a plugin.

ScreenHunter 65 Aug. 22 07.45 How To Add a Pinterest Button In WordPress Without Using A PluginThis tutorial is made by Marvie Pons of TutsKid and it’s pretty simple and easy to follow. Like with most, if not all WordPress tricks, all it takes is appending a few lines of code in your functions.php file. Simply copy and paste the code below:

function triptripper_pinterest_button($content) {
 global $post, $posts;
 // get image
 if(function_exists('get_post_thumbnail_id')){
 $catch_imgs = wp_get_attachment_image_src(get_post_thumbnail_id($post->id), 'large');
 $catch_img = $catch_imgs[0];
 }
 // If there is no featured image or any image, search post for images and display first one. If none exist, display default image
 if($catch_img[0] == ''){
 $out = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $match);
 if ( $out > 0 ){
 $catch_img = $match[1][0];
 } else {
 $catch_img = "/images/default.jpg";
 }
 }    
 $get_title = get_the_title();
 $get_link = get_permalink();
$content .= '
<div id="pinterest-wrapper"><a href="http://pinterest.com/pin/create/button/?url='.get_permalink($post->id).'&media='. $catch_img.'&description=' . $get_title . ' on ' . $get_link . '" count-layout="vertical">Pin It</a></div>
';
return $content;
}
add_filter('the_content', 'triptripper_pinterest_button');
function triptripper_enqueue_script() {
 wp_enqueue_script('pinterest', 'http://assets.pinterest.com/js/pinit.js', array(), null, true);
}
add_action ('wp_enqueue_scripts','triptripper_enqueue_script');     

The first function automatically gets the first image from the post to be pinned and the post’s permalink. And just like with the plugin, it will let you edit the description and the URL before posting. The second function adds the Pinterest JavaScript in your blog’s footer.

If you want to put the horizontal or no count share button, you can simply change the count-layout parameter to horizontal or none. And if you only want to show the button on single posts, pages, and attachment pages, just add the following tag on line 21:

If(is_singular()) {

Then close it with the curly bracket on line 25.

Incoming search terms for the article:

Related Posts

How to Create an Appealing Coming Soon Page

How To Create A Basic Social Sharing Button

How to Create Text with Flaming Meteor Effect

Creating Shiny Retro Text

1 Comment

  1. Bijutoha

    09.06.2012

    Could you tell me please how will i get functions.php file from my free wordpress site ?