Created
March 6, 2012 04:46
-
-
Save tollmanz/1983620 to your computer and use it in GitHub Desktop.
Pinterest image functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( $image = zdt_get_social_image( $post ) ) : ?> | |
<div class="pin_share"> | |
<a href="http://pinterest.com/pin/create/button/?url=<?php echo urlencode( esc_url( get_the_permalink( get_the_ID() ) ); ?>&media=<?php echo urlencode( esc_url( $image ) ); ?>&description=<?php echo urlencode( esc_html( get_the_excerpt( get_the_ID() ) ); ?>" class="pin-it-button" count-layout="none">Pin It</a> | |
</div> | |
<?php endif; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get the image for use with social media when status is transitioned. | |
* | |
* @param $new_status | |
* @param $old_status | |
* @param $post | |
*/ | |
function zdt_save_social_image( $new_status, $old_status, $post ) { | |
if ( $post->post_type == 'post' && $new_status == 'publish' ) { | |
update_post_meta( $post->ID, '_zdt_social_image', zdt_determine_first_image( $post, 'http://placehoneybadger.com/g/200/300' ) ); | |
} | |
} | |
add_action( 'transition_post_status', 'zdt_save_social_image', 10, 3 ); | |
/** | |
* Get the first image for the post. | |
* | |
* If there are any attachments associated with the post, get the first attachment and use that. | |
* Unfortunately, not all posts use an attachment related to the post. In this case, use the DOMDocument | |
* parser to find the first image in the post content and use that. If all else fails, use the default image | |
* sent to the function. | |
* | |
* @param $post | |
* @param bool|string $default | |
* @return string | |
*/ | |
function zdt_determine_first_image( $post, $default = false ) { | |
if ( is_object( $post ) ) | |
$post_id = $post->ID; | |
else | |
return esc_url( $default ); | |
// Start by getting an attached image | |
$attachments = new WP_Query( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_status' => 'inherit', 'posts_per_page' => 1, 'orderby' => 'menu_order' ) ); | |
if ( $attachments->have_posts() ) { | |
$id = $attachments->posts[0]->ID; | |
wp_reset_query(); | |
if ( $image = wp_get_attachment_image_src( $id, 'medium' ) ) { | |
return esc_url( $image[0] ); | |
} | |
} | |
// Provide our own error handling in the event that parse errors occur | |
libxml_use_internal_errors( true ); | |
// No attachments, so try grabbing the first image from the content | |
$doc = new DOMDocument; | |
if ( ! empty( $post->post_content ) && $doc->loadHTML( $post->post_content ) ) { | |
/** | |
* Detect fatal errors. In many cases, "simple" or "recoverable" errors may be thrown when parsing the HTML (http://www.xmlsoft.org/html/libxml-xmlerror.html). | |
* For instance, if two element have the same ID, an error of level 2 will be thrown. This is ok for our purposes; however, if a fatal | |
* error (level 3) is thrown, we should not attempt to do anything else with the object. | |
*/ | |
$fatal_error = false; | |
foreach (libxml_get_errors() as $error) { | |
if ( $error->level > 2 ) | |
$fatal_error = true; | |
} | |
if ( ! $fatal_error ) { | |
$images = $doc->getElementsByTagName( 'img' ); | |
if ( $images && $images->length > 0 && $images->item(0)->hasAttribute( 'src' ) ) { | |
$img_src = $images->item(0)->getAttribute( 'src' ); | |
return esc_url( $img_src ); | |
} | |
} | |
} | |
libxml_clear_errors(); | |
return esc_url( $default ); | |
} | |
/** | |
* Attempts to get image for social media. | |
* | |
* In the event that it is not already set, the image is obtained, saved and displayed. | |
* | |
* @param $post | |
* @param bool $default | |
* @return mixed|string | |
*/ | |
function zdt_get_social_image( $post, $default = false ) { | |
if ( $image = get_post_meta( $post->ID, '_zdt_social_image', true ) ) | |
return $image; | |
else { | |
$image = zdt_determine_first_image( $post, $default ); | |
update_post_meta( $post->ID, '_zdt_social_image', $image ); | |
return $image; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment