Add ‘Pin it’ to WordPress Theme
Pinterest has a ‘Pin It’ button available for websites in the ‘Goodies‘ section now. It’s much like the Twitter and Facebook buttons you see EVERYwhere. I added a bit so you can use it in your WordPress theme.
With the following code this button can go on your index, single or archive pages – anywhere the WordPress loop appears. It will automatically pull the first image from the post to be pinned and link to the post’s permalink. The title of the post along with the website url will appear in the description field but can be edited before it’s posted.
Place this code in your template where you want the ‘Pin It’ button to appear – it should be inside The Loop.
<a href="http://pinterest.com/pin/create/button/?url=<?php the_permalink() ?>&media=<?php echo pinterest_image() ?>&description=<?php the_title(); ?> on <?php bloginfo('url'); ?>" class="pin-it-button" count-layout="horizontal">Pin It</a>
Place this script in your footer.php file, just before the closing tag.
<script type="text/javascript" src="http://assets.pinterest.com/js/pinit.js"></script>
Put the following code in your functions.php file.
function pinterest_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}
You can change /images/default.jpg to whatever you’d like. This image will be pinned if there’s not an image in the post.
Be really careful in your functions.php file, it’s a little touchier than what you might be used to. The first thing I ever broke in one was putting a space after the closing ?> – it’ll break your whole site! Always back up your files before you start changing things.
Easy as that, happy pinning!
P.S. Before you ask a question about your WordPress theme please do a quick google or codex search – If you still can’t figure it out ask away and I’ll try my best to help when I can!
.
UPDATE!
Due to Pinterest’s huge popularity now, there are tons of plugins available that will do this for you. I just did a quick search and this one looks good. WP Beginner also shows a different method that doesn’t involve the function file.
Tags: code, pinterest, tutorial, wordpress

4×6 Monthly Booklet Tutorial
4×6 Index Card Folder Tutorial
DIY Cardboard Binder 





oopsy. I tried this without success, but I am not real up on how it all fits together.
I am using your Paper theme. Can you send me what the functions.php file was before I messed it up?
Thanks for this! You just saved me a heap of time.
Hey there Amanda,
Thank you for this, it works great!
On my blog all the little social icons float around at the bottom of the post. Is there any way to adjust the spacing between them so they can be nicely aligned like the way you have it in your blog?
Thank you for help.
Thanks for your help. Everything seems to be working ok except the Pinterest Counter does not work. It does not seem to update. I even clicked your yesterday and today it is still showing 5. Any ideas on this? Thanks!
Sorry, can’t help you. That would be an issue on the Pinterest side of things.
Hello
Were you able to get the counter working? I am having the same problem
Lewis
Thank you so much for this! I have one question: is there a way to make it so that the user can choose the image to be pinned, and not just the first image in the post?
Unfortunately that’s not an option at this time, or at least not that I know of. Maybe one day when Pinterest gets their API up and running!
Hi Amanda. Thanks for posting this but I’m having a slight problem. I don’t have an image come up when I use the button. I’m running the Genesis theme so I have scoured the internet and Studio Press’s forums and can’t seem to find a different function to call the first image in the post using a genesis specific hooks or function. What do you think??? Theme/framework related or some other issue???
It seems the link part works just fine but it’s not even recognizing my back up image & I’ve triple checked the location. I’m at a loss.
Hi! I don’t have much experience with Genesis and their loops but… did you see this post?
http://designsbynickthegeek.com/tutorials/genesis-explained-image-functions
It might help a bit. You don’t need to add a function because it’s already built into the framework I believe. I’d try to find the snippet that pulls your first attached image and insert it into the pin it link. Maybe genesis_get_image()? Good luck! :)
thanks for this info. worked like a charm, but now i just need to style it.. thanks again.
[...] Design, which just uses the JavaScript code from the Pin It web browser bookmarklet. I also tried a method that displays the number of shares, but couldn’t get it to work (the number of shares never [...]
Awesome – thanks so much for sharing this code – just what I needed!
I can’t wait to try this! Thanks for reminding me to back up my files!
This is great. Thanks for the script, real time saver!
Hi, Ive added the pin it button but, it’s not showing the post image, can you help?
Looks like you figured it out?
[...] originally found this code on a site called Ahhh Design which has quite a few interesting articles. Make sure to check them [...]
[...] 2) Via a modification to the functions.php file of your theme via [ahhh-design] [...]
[...] originally found this code on a site called Ahhh Design which has quite a few interesting articles. Make sure to check them [...]
Great post, but the javascript doesn’t need to be loaded for each post.
The row:
Should be added to footer.php (just before the closing -tag. That way it takes care of all the pinterest buttons once instead of for each item in the loop.
Cheers.
You are absolutely right, thank you!!
I follow your code but seem not work at my site.
So I edit it like this:
<a href="http://pinterest.com/pin/create/button/?url=&media=Image($post->ID,”url”,”&w=200″); ?>&description= on ” class=”pin-it-button” count-layout=”horizontal”>
And the function pinterest_image() , I don’t use.
Thanks Very Much.
[...] Add “Pin it” to WordPress Theme – from Ahhh-Design [...]
Any idea how to use this with your own custom pinterest button instead of the standard one?
I haven’t seen that anywhere, it must be possible though. I’ll keep an eye out for how to do it.
You seriously RULE!!!!! Works PERFECT. Thank you so much for this.
Thanks so much! Worked like a charm.
[...] to ahhh-design, they suggested downloading the Pinterest Pin-It WordPress plugin which you see at the top and [...]
[...] Design, which just uses the JavaScript code from the Pin It web browser bookmarklet. I also tried a method that displays the number of shares, but couldn’t get it to work (the number of shares never [...]