Use PHP to block Pinterest “pins” and still let your site validate
Prevent Pinterest users from “pinning” images on specific web pages or right across your website with this simple PHP include.
Pinterest is quickly becoming a major force in social media. The website acts as a virtual notice board, allowing its users to “pin” interesting images in websites and then share them with the world.
Pinterest has come under fire because many people believe that the site breaches copyright by distributing copies of peoples’ images rather than linking to them like Twitter. For example, this pin of mine references this Flickr image, but as can be seen from the source code the image is hosted on Pinterest servers. If I delete the Flickr file, a copy remains on Pinterest in perpetuum. Piracy was rife and for this reason, Flickr blocked the site (but has since reversed its decision). Pinterest became concerned about the piracy issue and so took steps to remedy it.
Pinterest now provides a meta tag that can be used to block Pinterest from taking images off your web server, however this code doesn’t validate:
The best remedy is to use PHP to detect the Pinterest user-agent, and then conditionally echo the invalid meta tag. Aside from the Pinterest website, no other users will see it so whilst the code isn’t strictly valid, your site will still pass the W3C validator.
Here’s what to do. Create a plain text file with the following code:
"; } ?>
Save the file as nopin.php.
According to Ben Werdmuller, the current user agent name is “Pinterest/0.1 “. This code is designed to detect any user agent with “Pinterest” in its name (so if it changes to “Pinterest/0.2 “, it will still be caught).
Place the following PHP include code within the <head> of your web pages that you wish to protect. If you have a content management system or a website that uses site-wide templates, you can add the include to the appropriate template file.
Now when users attempt to “pin” your website, they will be greeted with the standard Pinterest error message stating “We couldn’t find any images. This site does not allow pinning”.