Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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