Skip to content

Instantly share code, notes, and snippets.

@jaredatch
Created July 8, 2014 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredatch/027f611dda00431253ab to your computer and use it in GitHub Desktop.
Save jaredatch/027f611dda00431253ab to your computer and use it in GitHub Desktop.
Dynamically create Pinterest Pin button for content images
<?php
/**
* Inline javascript
*
* @since 1.0.0
*/
function ja_pinterest_button() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
if ( $( window ).width() > 700 ) {
$('.entry-content img').each(function(index, el) {
if ( !$(this).hasClass('no-pin') && !$(this).hasClass('alignleft') && !$(this).hasClass('alignright') ) {
media = $(this).prop('src');
link = '<a href="http://pinterest.com/pin/create/button/?url=<?php echo urlencode( get_permalink() ); ?>&media=' + media + '&description=<?php echo urlencode( get_the_title() ); ?>" class="pin-it-button">Pin</a>';
$(this).wrap('<div class="pin-image-wrapper"></div>').after(link);
}
});
}
});
</script>
<?php
}
add_action( 'wp_footer', 'ja_pinterest_button' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment