Search code examples
phpjavascriptpinterest

Can I use PHP to dynamically insert url, media and description with Pinterest?


Here's my code:

<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;
    if (window.location.protocol == "https:")
        s.src = "https://assets.pinterest.com/js/pinit.js";
    else
        s.src = "http://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>

 <!-- Customize and include for EACH button in the page -->
  <a href="http://pinterest.com/pin/create/button/?url=<?php echo $this->helper('core/url')-       >getCurrentUrl(); ?>&media=<?php echo $this->helper('catalog/image')->init($_product, 'image')->resize(500); ?>&description=<?php echo $_product->getShortDescription(); ?>" class="pin-it-button" count-layout="none">Pin It</a> 

The php successfully gathers all of the elements and when I click on the button, it pops up the modal window.

However, in the subsequent modal window, it does not pin to my board when I'm logged in.

The Script tag and link are both inside of a list item.

Any help would be appreciated.

Thanks, Moises M.


Solution

  • From your description it sounds like the problem is that you are not url encoding the parameters, i.e. everything after the ?url=

    I'm not sure if that's the only problem you'll run into with this, but it should fix the one you're currently hitting!