|
// Wraps image with container and add link to share src from all <img> in the content |
|
function gear_pinterest_in_content_images($content) { |
|
|
|
$dom = new DOMDocument('UTF-8'); |
|
@$dom->loadHTML( utf8_decode($content) ); // Decode to simple ISO to avoid accent errors |
|
$dom->preserveWhiteSpace = false; |
|
$images = $dom->getElementsByTagName('img'); |
|
|
|
if( count($images) > 0 ) { |
|
foreach ($images as $image) { |
|
// get the widths of each image |
|
$width = $image->getAttribute('width'); |
|
|
|
// if image is less than 860, add their old classes back in plus our new class |
|
if( $width >= 860) { |
|
|
|
$clone_img = $image->cloneNode(); //Clonamos el nodo de la imagen |
|
|
|
// Build Pinterest URL format |
|
$pin_url = sprintf('http://pinterest.com/pin/create/button/?url=%s&media=%s&description=%s', |
|
urlencode(get_permalink()), |
|
$image->getAttribute('src'), |
|
urlencode(get_bloginfo('name').' - '.get_the_title()) |
|
); |
|
|
|
// Create DIV container |
|
$pin_wrap = $dom->createElement('div'); |
|
$pin_wrap->setAttribute('class', 'pinterest-button' ); |
|
|
|
// Create link to share with no content and attributes |
|
$pin_link = $dom->createElement('a',' '); |
|
$pin_link->setAttribute('class', 'share-pinterest icon-pinterest' ); |
|
$pin_link->setAttribute('title', __('Compartir en Pinterest','tindas') ); |
|
$pin_link->setAttribute('href', $pin_url ); |
|
|
|
// Add to image to new container |
|
$pin_wrap->appendChild( $clone_img ); |
|
|
|
// Add Pinterest link to container |
|
$pin_wrap->appendChild( $pin_link ); |
|
|
|
// Replace img object with all new elements |
|
$image->parentNode->replaceChild( $pin_wrap, $image ); |
|
} |
|
} // end if count |
|
|
|
$content = $dom->saveHTML(); |
|
} |
|
return $content; |
|
} |
|
add_filter( 'the_content' , 'gear_pinterest_in_content_images' , 15 ); |
Very nice