Share Images with a Pinterest Call-to-action
In this article, I’ll demonstrate how to add a Pinterest call-to-action on top of an image when a user is actively interacting with it.
Firstly, let’s look at why you would even want to do that.
Sharing Is Caring
What constitutes good content often depends a lot on the particular needs and interests of end users. Good content might be attractive, effective, simple, functional, informative, inspiring … and so on.
We are used to adding social network buttons or links to share a page (ideally via fat-free buttons). Facebook, Twitter and Google+ are often the primary must-haves.
We may also want users to be able to share parts of our page. Page content is often made of chunks, and each of these chunks can be provided with its own preferred social network sharing feature.
For instance, a quote can be targeted to fit Twitter’s 140-character limit, and a picture can be aimed for Pinterest.
Let’s look at how you set up your images to be shared on Pinterest.
Anatomy of a Pin URL
A Pinterest Pin URL is made of four components:
- the Pinterest part of the URL:
https://www.pinterest.com/pin/create/button/
- the
url
parameter: the address of the page to link to, URL-encoded - the
media
parameter: the address of the picture to share, URL-encoded - the
description
parameter: the descriptive text shown on Pinterest, URL-encoded, 500 characters max, but 200 characters seems to be the best choice.
Some HTML data elements can be added if you add Pinterest’s own JavaScript, especially data-pin-do
and data-pin-config
, but it won’t be needed in this case, as this is a fat-free approach where we rely on the strict minimum amount of code.
The Hard-coded Way
The code we want to get looks like this:
<a href="http://www.pinterest.com/pin/create/button/?url=<page url (encoded)>&media=<picture url (encoded)>&description=<HTML-encoded description>" target="_blank" class="pinterest-anchor pinterest-hidden">
<div class="pinterest-logo"> </div>
</a>
<img src="<picture url>" data-pin="pinIt" alt="<description>">
Each sharaeble picture (marked with the data-pin
attribute pinIt
) is prefixed with an anchor to the Pinterest sharing page with the right attributes in its URL, embedding a div
to display the logo.
Three CSS classes are used here: pinterest-anchor
, which absolutely sets the Pinterest logo position on the top-left corner of the picture; pinterest-hidden
, which sets the visibility status of the logo, and pinterest-logo
, the logo itself, 40×40 pixels, encoded in base64 in the CSS.
.pinterest-anchor {
margin-left:10px;
margin-top:10px;
position:absolute;
z-index:42;
width:40px;
height:40px;
}
.pinterest-hidden {
display:none;
}
.pinterest-anchor:hover {
display:block !important;
}
.pinterest-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Uz0AAAAASUVORK5CYII=") no-repeat center center;
background-size: 40px 40px;
height: 100%;
width: 100%;
}
The animating part – showing and hiding the logo on hovering the picture – is handled in JavaScript, using jQuery notation:
$("img[data-pin='pinIt']").each(function() {
$(this).hover(function() {
$(this).prev().css("display", "block")
}, function() {
$(this).prev().css("display", "none")
});
});
Automation Is Your Friend
But I don’t want to bloat my content code with that specific piece of HTML that prefixes the sharaeble picture. The maintenance side of it alone will have some cost if anything has to change – such as the Pinterest share URL, parameters, and support for other social networks.
As JavaScript is already used to handle the mouse interaction, let’s go one step further and generate that code for any image on the page.
Using jQuery again, we get:
$("img[data-pin='pinIt']").each(function() {
$(this).before('<a href="http://www.pinterest.com/pin/create/button/?url=' + encodeURIComponent($(location).attr("href")) + '&media=' + encodeURIComponent($(this).attr("src")) + '&description=' + encodeURIComponent($(this).attr("alt")) + '" target="_blank" class="pinterest-anchor pinterest-hidden"><div class="pinterest-logo"> </div></a>');
$(this).hover(function() {
$(this).prev().css("display", "block")
}, function() {
$(this).prev().css("display", "none")
});
});
For each image with the data-pin
attribute set to pinIt
, we prefix it with the sharing anchor, getting the page URL directly from the page location
attribute, the picture URL from the image href
, and the description from its alt
text, while properly encoding the URI components.
This is demonstrated in the following CodePen
See the Pen Pinterest pin-it Call-To-Action (jQuery) by SitePoint (@SitePoint) on CodePen.
But Do We Need jQuery?
The fact is, jQuery might not be needed. If you don’t really need it, plain old vanilla JavaScript can do the job. As a matter of fact, I consider it a good exercise to translate jQuery code chunks into regular JavaScript from time to time. It’s a reminder of how everything works behind the curtain.
var pinables = document.querySelectorAll('img');
Array.prototype.forEach.call(pinables, function(el, i){
data = el.dataset;
if (data.pin=='pinIt') {
el.insertAdjacentHTML('beforebegin', '<a href="http://www.pinterest.com/pin/create/button/?url=' + encodeURIComponent(window.location.href) + '&media=' + encodeURIComponent(el.src) + '&description=' + encodeURIComponent(el.alt) + '" target="_blank" class="pinterest-anchor pinterest-hidden"><div class="pinterest-logo"> </div></a>');
el.onmouseover = function(){ this.previousSibling.style.display='block'; }
el.onmouseout = function(){ this.previousSibling.style.display='none'; }
}
});
The images selector – $("img[data-pin='pinIt']").each
– is now an array of all images for which we check the data attribute value. The rest of the code is a direct translation of the jQuery implementation.
As you can see in this Pen, it works exactly the same.
See the Pen Pinterest pin-it Call-To-Action (javascript) by SitePoint (@SitePoint) on CodePen.
Where To Go From There?
In web design and user experience, simplicity is the way to go. We don’t want to clutter our interfaces with social network logos. In that sense, this approach is very clean: the logo appears only when the user hovers over the image; and the Pinterest logo is well-enough known that we don’t need to add a text link next to it to explain its meaning.
However, the sharing form opens in a new window, which drives the user outside of the current page. As Craig Buckler states in his article, this “is a step too far for the most eager social media manager”. A small pop-up window to show it can be a viable alternative, for which he gives a vanilla JavaScript implementation.
For those interested, a small hint here: in our case, the target has no href
attribute, but its parent node has one. (You can check out a real-life implementation of this.)
So, what are your thoughts on social network buttons? Do you favor the fat-free approach, or would you rather utilize all of the features that JavaScript can offer? Let us know in the comments!