Last active
September 2, 2016 12:31
-
-
Save shackep/e69a4b67387f4583b5ea999b8dcbaf3f to your computer and use it in GitHub Desktop.
Get first attached image in post content and set as featured image.
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 | |
function autoset_featured( $new_status, $old_status, $post ) { | |
// Only run on manual saves of posts that are being published or have been published. | |
if ( $new_status != 'publish' || wp_is_post_autosave( $post->ID ) || $post->post_type != 'post' || empty( $post->post_content || has_post_thumbnail( $post->ID ) )) { | |
return; | |
} | |
// Is there already a thumbnail set? | |
$already_has_thumb = has_post_thumbnail( $post->ID ); | |
// Yes. Ok, we are done here | |
if($already_has_thumb){ | |
return; | |
} | |
// No. Is there an image in post_content? | |
$imageclass =''; | |
$dom = new DOMDocument(); | |
@$dom->loadHTML( $post->post_content ); | |
$search_dom = new DOMXPath($dom); //allows xpath queries like the one below | |
$foundimg = $search_dom->query('//img'); // find first image | |
// No image? Ok, we are done here | |
if (0 === $foundimg->length ) { | |
return; | |
} | |
// Yes image exists | |
// Used to check if maybe attachment and against existing attachments | |
$imageclass = $foundimg->item(0)->getAttribute( 'class' ); | |
// Was it added as a standard attachment? Let's check for 'wp-image-' in the class. | |
if(strpos( $imageclass, 'wp-image-' ) == FALSE ){ | |
// No. Side-load the image and attach it | |
$image_src = $foundimg->item(0)->getAttribute( 'src' ); | |
$tmp = download_url( $image_src ); | |
$file_array = array( | |
'name' => basename( $image_src ), | |
'tmp_name' => $tmp | |
); | |
if ( is_wp_error( $tmp ) ) { | |
@unlink( $file_array[ 'tmp_name' ] ); | |
return $tmp; | |
} | |
// Used media_handle_sideload as media_sideload_image did not return ID of attachment. | |
$added_attachment = media_handle_sideload($file_array, $post->ID); | |
set_post_thumbnail( $post->ID, $added_attachment ); | |
return; | |
} | |
// Yes it was attached. Let's check the attachments and set the match as thumbnail | |
if ( ! $already_has_thumb ) { | |
$attached_image = get_attached_media( 'image' ); | |
// Compare each attached image with the first image retrieved from post_content | |
if ( $attached_image ) { | |
foreach ( $attached_image as $attachment_id => $attachment ) { | |
// Check if an attached image matches the first image found in content | |
$current_attachment = 'wp-image-' . $attachment_id; | |
$first_attached_image = strpos( $imageclass, $current_attachment ); | |
if ( $first_attached_image !== FALSE ) { | |
// If match found, attach to post as thumbnail/featured image | |
set_post_thumbnail( $post->ID, $attachment_id ); | |
return; | |
} | |
} | |
// Last ditch effort to find an attachment | |
$guess_attach_id = explode( ' wp-image-',$imageclass ); | |
$maybe_attachment = $guess_attach_id[1]; | |
if(wp_attachment_is_image( $maybe_attachment )){ | |
set_post_thumbnail( $post->ID, $maybe_attachment ); | |
} | |
return; | |
} | |
} | |
} | |
add_action( 'transition_post_status', 'autoset_featured', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment