Do you want to add the Pinterest “Pin It” button in WordPress? Recently, a new traffic source popped up in our blog stats.
It was significant enough for us to take notice. This traffic source was Pinterest.
Pinterest is a popular social networking site that allows you to share visual content and drive a lot of traffic to your website (follow WPBeginner on Pinterest).
In this article, we will show you how to add the Pinterest “Pin It” button to your WordPress blog. We will also explain how to add the Pin It button over the images of your website.
Adding a Pinterest “Pin It” Button Using a Plugin
Pinterest officially changed the “Pin It” button name to Save in 2016.
Although some plugins still use the name “Pin It” instead of Save, they do the same job of pinning your images to Pinterest.
The easiest way to add a Pinterest button to your WordPress site is by using a social sharing plugin.
We recommend using Shared Counts plugin for this purpose. It is the best social media plugin for WordPress because it’s free and allows you to easily add the Pinterest button along with other popular social networks.
To get started, you need to install and activate the Shared Counts plugin. You can follow our step by step guide on how to install a WordPress plugin for detailed instructions.
Upon installation, head over to Settings » Shared Counts page to configure the plugin.
On the settings page, you need to scroll down to the Display section and then click on the “Share Buttons to Display” textbox.
This will open a dropdown menu where you can select the social media services you want to add. By default, Pinterest will be present in that box, along with Facebook and Twitter.
You can also select the location and post type where you want to display the Pinterest button. It comes with multiple button styles that you can choose from the ‘Share button style’ option.
Once you are done, don’t forget to click on the Save Changes button to store the settings.
You can now visit any post on your website to see the Pinterest button in action.
Another neat thing about SharedCounts is the free Custom Pinterest Image addon available on Github. This lets you set a custom Pinterest sharing image and description that’s optimized for Pinterest.
Note: We use SharedCount plugin on our website because it’s the best option in the market. It’s very well-coded and it’s free.
Manually Adding a Pinterest Pin It Button in WordPress
Some intermediate users may prefer to add the social sharing buttons manually to their WordPress site instead of using a plugin.
Let’s take a look at how to manually add a Pinterest button in WordPress.
The first thing you need to do is make a complete WordPress backup of your site. This will help you restore your site in case something breaks by accident.
Next, you need to connect to your WordPress hosting using an FTP client and then go to the /wp-content/themes/ folder.
From here, you need to open your current theme folder and then locate the footer.php file.
Next, simply right-click on that file and then select Download from the menu. This will download the footer.php file to your computer.
Now you need to open the footer.php file using a plain text editor like Notepad and then paste the following script right before the </body>
tag.
<script type="text/javascript"> (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 = "https://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>
Once you have done that, you need to save the file and then upload it back to the current theme folder.
Next, you need to locate and download the single.php file from your theme folder and open it for editing. After that you need to add the following code.
You will need to choose the location where you want it to appear in your post. After post title is the most common placement for social media buttons.
<?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>
The above code displays the Pinterest button with your featured image, title, description, and post URL in the share URL parameter.
Finally, you need to save the file and upload it back to your theme directory using FTP.
This will add a vertical share button to the posts of your website. If you want to display the horizontal share button, then simply change the count-layout
parameter to horizontal.
Creating a Shortcode for Pinterest Button
Recently, one of our users asked us how to create a shortcode for the Pinterest “Pin It” button.
Shortcodes in WordPress allow you to easily add dynamic items to your WordPress posts, pages, and widgets. By creating a shortcode, you’ll be able to manually add Pinterest button in your articles.
First, you’ll need to add the following code to your theme’s functions.php file or a site-specific plugin:
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('pin', 'get_pin');
After that, you can use the [pin] shortcode in your WordPress posts where you want to display the Pinterest button.
Adding Pinterest Pin It Button Over Your Images
If you are running a fashion, photography, or portfolio site, then you definitely want your images to be shared on Pinterest to increase your website traffic.
Let’s take a look at how to add the Pinterest Pin it button over your images in WordPress. This button will appear only when the user brings their mouse over your images, allowing them to easily share the image on their Pinterest profile.
First, you need to install and activate the Pinterest Pin it Button for Images plugin.
Upon activation, go to Settings » Pinterest Pin It page to configure the plugin.
On the settings page, you can choose where you want to show the “Pin It” button on your WordPress site. You can display it only on single posts, pages, category pages, or everywhere on your site.
The next option is to choose the default description from the dropdown menu. This will be used as the image description for the pinned image.
Once done, you need to click on the Save button to store the settings. Now you can visit your website and then hover your mouse cursor over any image to see the Pinterest “Pin It” button.
If you want to exclude some images from showing the Pin It button, then you can add the nopin CSS class to the images you want to exclude.
To do that, you need to select the image block in the post editor and then click on the 3 vertical dots icon from the block toolbar. This will open a menu where you need to click on the Edit as HTML option.
You will now see the HTML code of your image. Go ahead to add nopin to the class attribute of the img tag:
<figure class="wp-block-image"><img src="https://example.com/wp-content/uploads/2019/06/imagename.jpg" alt="image name" class="nopin wp-image-1687"/></figure>
Similarly, you can also use the plugin to disable automatic display of the “Pin It” button, and use the CSS class “pinthis” to selectively display the button on specific images.
To do that, you need to visit the plugin’s settings page and then locate the Advanced section on the right side of the page.
Now you need to check the option that says ‘Show “Pin it” button only on images with class=”pinthis” ‘, and then click on the Save button.
The next time you add an image to a post or page, you need to select the image block and then use the Edit as HTML option to add the pinthis CSS class to the image code. Here is an example:
<figure class="wp-block-image"><img src="https://example.com/wp-content/uploads/2019/06/imagename.jpg" alt="image name" class="pinthis wp-image-1687"/></figure>
This will show the Pinterest button on that specific image, even if you have disabled the option to automatically show the Pin It button on your site.
That’s all!
We hope this article helped you understand how to add a Pinterest “Pin It” button to your WordPress blog. You may also want to see our guide on how to create an email newsletter and get more subscribers.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
After I installed the plugin, it never showed up in the settings.
You would want to ensure you activate the plugin as well.
This didn’t work
It would depend on what method you used for how to try to fix the error. You would either want to reach out to the plugin’s support or use our contact form to reach out to us.
Thanks. It works.
You’re welcome, glad it worked for you
hi wpbeginner, I follow your codes, may i know if i will still pin the images on my pinterest boards? or it will automatically pin there? Im still a beginner in this platform. thank you
The button when used would add it to your board.
Thanks for the code. I just added it to my author site. (I have no idea how the Pinterest button shows up on my images on my business site, but it does.)
Now my author site is all set up!
Thanks, Syed.
P.S. I just followed you on Pinterest.
Hey Lorraine,
Thanks for following us on Pinterest. You can also find us on Twitter and Facebook.
this is work !! thanks
and
I wonder…
how can I change icon? Icon is still same when change code
I did the last option, adding the short code to my theme’s functions.php file. It worked, but I couldn’t save my posts as draft anymore. Then I removed the code and it is not working. My site is still online, but I cannot log in into my site anymore. The following message appear:
Warning: Cannot modify header information – headers already sent by (output started at /home/peque107/public_html/wp-content/themes/himmelen/functions.php:2) in /home/peque107/public_html/wp-includes/pluggable.php on line 1224
Please help!
Thanks. Its great
Does this only apply for wordpress.org blogs? I have a wordpress.com blog and I am not sure if I can add this plugin to it?
Thank you!
Yes it is for WordPress.org. Please see our guide on the difference between self hosted WordPress.org vs free WordPress.com blog.
I have a problem. I followed the instructions and came up with this error:
Parse error: syntax error, unexpected ‘<' in /home/cmomb/butfirstwehavecoffee.com/wp-content/themes/notepad/functions.php on line 18
Now I cannot get rid of it. Could someone advise. My site is now down.
Open the functions.php file in a text editor like notepad. Go to line 18. You probably have <?php tag there that you don’t need. Delete it and also delete the ?> closing tag
Thanks for this firstly
I am struggling to position the element. It is seeming to always sit in the top left of the div.
Is it possible to position it and also change the bg image to own custom one.
Thanks
Great tutorial, as usual. I was wondering though if there was a way to modify it to use a custom button in place of the starboard one that automatically comes up.
Thanks,
Mark
Good tutorial, i have put it on my site without any problems, i then tested it and it worked, but it still says 0 after i pinned.
Mark
I tried so many plugins… they don’t work with infinite scroll and nextgen gallery…. but with some changes this script solved my issue.. thanks
Hello,
I just want to ask if I could change the size of the “pin it” button? Because it seems that it was a little bit small.
Regards,
Michael
You could use the pin count layout vertical or horizontal. Other than that, no you cannot change the size.
This code just recently stopped working. I had it on my site and it was working great and recently images appear to be working but then when you go view the pinboard there is no image set. Other times when trying to pin it 502 error that comes from the Pinterest site. Any ideas?
Probably server errors on Pinterest’s end. We have this code running on our other sites.
There’s a bug in the code. The generated href for the pin it button should include ‘url=’, currently it’s missing the equals sign.
Fixed the shortcode code for that. Thanks for reporting it
For wordpress users..adding the pin it or any other social media is easy! Under dashboard, go to setting, click on sharing and they all appear- click on what you want to add!
You are referring to WP.com sites. This tutorial is for self-hosted WordPress sites. Two different things.
THANK YOU! I have been searching for how to do this for 2 days & downloaded something & that didn’t work, I was about to give up & then THANKFULLY read your comment. I appreciate someone making it as EASY as it really is, how come wordpress can’t do that?
Ooh, thanks for the tip! Exactly what I was looking for
Is there a way to customize this so you can select a custom image for the pin-it button ?
If you mean a custom image for your article, then yes you can. Look at the &media tag in the second code.
Hi,
I need to implement this code but in header file, but postthumbnail returns error?!
@Husein Yuseinov You have to call the thumbnail code within your post loop. In order to call it in your header.php file, you must utilize the global variable.
https://www.wpbeginner.com/wp-themes/how-to-display-custom-fields-outside-the-loop-in-wordpress/
@wpbeginner Thanks for the fast respond, but I’m not a coder, and it’s hard for me to understand you.
@Husein Yuseinov Ok… so here is what you do. Add the following line above $pinterestimage line inside the PHP tags:
global $wp_query;
$postid = $wp_query->post->ID;
Then replace $post->ID with $postid and it will work. Also after the code, you may want to end wp_reset_query();
This is the best we can do. What you are asking for requires coding. If you are unable to do this, then shoot us an email. We can help you out for a small fee.
What in the world is going on with the “animhut blog” button on thee next 3 comments?
@robthecomputerguy I think they just liked our comments, and livefyre is showing their profile.
@wpbeginner Oh that’s fascinating – that didn’t even occur to me – thanks for the follow up!
I tried adding this… it worked but when you click on “Pin it” and it opens the window in a new browser and you actually submit the pin, it just reloads the newly opened window and the pin isn’t submitted. Decided not to implement it until they’ve fixed their bugs.
If you don’t want to mess with adding code, I created a Pin It button plugin you can try out.
http://wordpress.org/extend/plugins/pinterest-pin-it-button/
(or just search for “pinterest pin it” under plugins)
I used the ‘follow me’ code on the Pinterest Goodies page and pasted it into the ‘text’ widget on my WordPress blog. The button now appears right after my mini bio. It works quite well.
For any advanced theme framework like Thesis, Genesis, Headway etc, you have to add these codes via functions.php file using the framework appropriate hooks. We cannot possibly cover all the theme frameworks out there. Most theme framework blogs have instructions on how to do customize the themes.
Your method seems a bit tedious because you would have to do it for each image.
@wpbeginner It’s not ideal, but it’s really not much more than you have to do to post the image anyway. I’m still looking for a great option (plugin ideally) that a) works on my theme and b) is easy. Luckily, due to the popularity of Pinterest, I’m confident someone will have one up and running soon!
Make sure you set $pinterestimage[0] otherwise it will return an array. Other than that not sure why you are getting the error.
@wpbeginner It’s working now, thanks to the shortcode code you gave us. Thank you so much!
Now to get it to align with everything else via CSS… (http://ylcf.org/2012/01/tomorrow-2/) Ah, the joys.
@merrittsgret Yeah, you might have to utilize the !important tag in some cases.
Any idea on how to change it from featured image to first image?
@dave2 Use a variation of this snippet:https://www.wpbeginner.com/wp-themes/how-to-set-a-default-fallback-image-for-wordpress-post-thumbnails/
@wpbeginner @dave2 Thank you for the tutorial ! i need that for my another photography site
It works, but it shows the text “array” before the Pin It button. I’m calling it via my functions.php, like so “echo $pinterestimag…..<?php;" Could that be why?
I cannot seem to get it to work. I put the first code in my footer.php before the closing body tag and then added the other code into my single php after my other sharing buttons provided by po.st. Yet when I look at a post on my site (http://crockpotladies.com/recipe-categories/entrees/crockpot-chicken-enchiladas/) I do not see a Pin It Button. Any suggestions?
@Heidi at CrockPotLadies We can see the button in the next line after the Subscribe to feed etc links.
Only just started to hear about Pinterest – looks as though it is really taking off.
Thanks for the tut.
Thanks for this. I recently tried a similar method but it doesn’t seem to work with my WordPress.org theme, so I came up with my own work-around and posted it here: http://www.tomaytotomaaahto.com/tools-and-tutorials/how-to-turn-your-images-into-pinterest-pin-it-buttons-wordpress
How do you get this to integrate with an existing WP plugin like Digg Digg or Sociable?
@YeahThatsKosher You would have to ask the developers to do so for you.