Is there a way to use your module so we can include a Pinterest button on every article and have it populate the Pinterest button with the proper canonical URL and primary image that is associated with every article?

Comments

mikefyfer’s picture

Just an fyi, something like this will do the trick. You'll put it in a block, or in a tpl, depending on what you're comfortable with or your use case.

<a href="http://pinterest.com/pin/create/button/?url=<?php print $url; ?>&media=<?php print file_create_url(file_directory_path() .'/imagecache/whatever-size/'. drupal_urlencode(_imagecache_strip_file_directory($node->field_image[0]['filepath'])));
?>" class="pin-it-button" count-layout="horizontal">Pin It</a>
<script type="text/javascript" src="http://assets.pinterest.com/js/pinit.js"></script>

There are a bunch of ways to get your $url (that variable by itself won't do anything). It'll be different depending on if you're on d6 or 7 - see Drupal API

But if you're looking to just pop a 'pin it' button on some page, something like this should work.

rhip’s picture

Thank you mikefyfer.

I was wondering whether you would help me... I can't get this bit of the code to producing anything:

drupal_urlencode(_imagecache_strip_file_directory($node->field_image[0]['filepath'])

The whole thing is fine except the name of the image is missing (it spits out this: www.mysite.com/sites/default/files/imagecache/recipe-620/recipeImages">Pin It )

I have the following code in a block on a page:

<?php $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
if (!empty($_SERVER["QUERY_STRING"]))
$url .= "?".$_SERVER['QUERY_STRING'];
?>
<a count-layout="horizontal" class="pin-it-button" href="http://pinterest.com/pin/create/button/?url=<?php print $url; ?>&amp;media=<?php print file_create_url(file_directory_path() .'/imagecache/recipe-620/recipeImages/'.drupal_urlencode(_imagecache_strip_file_directory($node->field_image[0]['filepath'])));
?>">Pin It</a> <script type="text/javascript" src="http://assets.pinterest.com/js/pinit.js"></script>
mikefyfer’s picture

RhiannonAmyBuck,

Sorry, my post in comment #2 was for Drupal 6.

If you're on D7, something like this should do the trick to get your image path. Where field_main_image is the main image for your node.

<?php
$items = field_get_items('node', $node, 'field_main_image');
$image_uri = file_create_url($items[0]['uri']);
?>

Hope it helps.

p55mac’s picture

I put the following code into my node--[content type].tpl.php using Drupal 7

<a href="http://pinterest.com/pin/create/button/?url=<?php print url($node_url, array('absolute' => TRUE));?>
&media=<?php print render(file_create_url($node->field_image['und'][0]['uri'])); ?>&description=<?php print render($title); ?>%20<?php print render($node->field_year['und'][0]['safe_value']);?>"class="pin-it-button" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a>

<script type="text/javascript" src="//assets.pinterest.com/js/pinit.js"></script>

Of course you may have a different name for the field image that you want to be selected and of course the description field etc too.

Hope this helps anyone.

sachbearbeiter’s picture

#4 this solved me hours of research: file_create_url

thanks a lot ...

redhatmatt’s picture

Having trouble with the Drupal 6 example, actually getting the pinterest page to actually submit the pin... it works all the way until I actually click "pin it" which then starts to work but ends up doing nothing and displays a blank image on the same page. If I do the urls manually from the pinterest wizard/goodies page everything works.

See: http://dev.pencils.com/forestchoice-hardcover-reporter-small-ruled
look down the page to find the pinterest button right after the facebook like button after the product description.

I get the full url:

<?php $zurl = url(NULL, array('absolute' => TRUE)); ?>
<br />
<?php $fullurl = $zurl.$node->path;?>
full url and path: <?php print $fullurl; ?>

and add the badge and all the basic stuff:

<a href="http://pinterest.com/pin/create/button/?url=
<?php print $fullurl; ?>
&media=
<?php print file_create_url(file_directory_path() .'/imagecache/product_full/'. drupal_urlencode(_imagecache_strip_file_directory($node->field_image_cache[0]['filepath'])));?>" 
class="pin-it-button" 
count-layout="horizontal">
<img border="0" 
src="//assets.pinterest.com/images/PinExt.png" 
title="Pin It" /></a>

I'm breaking the code out so I can read it better, have been slow and methodical with a lot of troubleshooting. Please help ;)

redhatmatt’s picture

Never mind all that works just fine. I was working all this out for a long time (a few hours) and kept committing up to the server every so often with repeated failures. At some point I stopped committing just working locally. Finally when I posted the above code and question together to show an example it all worked when I tested a final time. Duh... the pinterest server couldn't communicate with my local when I finally did have the code right. Hopefully this helps someone out there... it's for Drupal 6.

redhatmatt’s picture

Spoke to soon. The images still don't show up as thumbs on http://pinterest.com/redhatmatt/products-i-love/

This would seem to be a pinterest problem but I know somehow I caused this originally, back to the drawing board. If anyone has any pointers on this part I'd love to hear em.

redhatmatt’s picture

Everything is fine now and I even added in the pre-filled description with the title. It just took pinterest awhile to either cache my images and spit them out or just catch up. Everything is now working swimmingly.

<?php $zurl = url(NULL, array('absolute' => TRUE)); ?>
<?php $fullurl = $zurl.$node->path;?>
<div class="pinterest">
<a href="http://pinterest.com/pin/create/button/?url=<?php print $fullurl; ?>&description=<?php print $title; ?>&media=<?php print file_create_url(file_directory_path() .'/imagecache/product_full/'. drupal_urlencode(_imagecache_strip_file_directory($node->field_image_cache[0]['filepath'])));?>" class="pin-it-button" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a>
</div>
ayushjn’s picture

Status: Active » Needs work

Looks like a Pin It button is what we are looking forward to ? . I will soon work on it.
Thanks all for the suggestion guys!!!

mealto’s picture

ok, just found all the great response to my original question, thanks so much. When I opened the thread, there was no one else talking about Pinterest. I need some help here. I am guessing I have to populate some fields:

http://pinterest.com/pin/create/button/?url=http://www.domain.com/&descr...

How do I populate the article URL and imagecache URL? I have this code in a block.

<?php $zurl = url(NULL, array('absolute' => TRUE)); ?>
<?php $fullurl = $zurl.$node->path;?>
<div class="pinterest">
<a href="http://pinterest.com/pin/create/button/?url=<?php print $fullurl; ?>&description=<?php print $title; ?>&media=<?php print file_create_url(file_directory_path() .'/imagecache/product_full/'. drupal_urlencode(_imagecache_strip_file_directory($node->field_image_cache[0]['filepath'])));?>" class="pin-it-button" count-layout="horizontal"><img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" /></a>

The imagecache image is a cached image of a Primary Image that we have inserted for each article. How do I grab this URL as well as the URL of each article?

Any help would be appreciated!

mealto’s picture

Bump. Anyone care to help out with some coding? Cannot get this to work :(