Tự động set ảnh đầu tiên là featured image cho module auto-upload-images
Sửa đoạn cuối, sau hàm foreach trong hàm save() trong /wp-content/plugins/auto-upload-images/src/WpAutoUpload.php
Và thêm hàm get_first_content_image_id trong file đó
private function get_first_content_image_id ( $content ) {
// set variables
global $wpdb;
// look for images in HTML code
preg_match_all( '/<[iI][mM][gG][^>]+>/', $content, $all_img_tags );
if ( $all_img_tags ) {
foreach ( $all_img_tags[ 0 ] as $img_tag ) {
// find class attribute and catch its value
preg_match( '/<img[^>]*class\s*=\s*[\'"]([^\'"]+)[\'"][^>]*>/i', $img_tag, $img_class );
if ( $img_class ) {
// Look for the WP image id
preg_match( '/wp-image-([\d]+)/i', $img_class[ 1 ], $found_id );
// if first image id found: check whether is image
if ( $found_id ) {
$img_id = absint( $found_id[ 1 ] );
// if is image: return its id
if ( wp_attachment_is_image( $img_id ) ) {
return $img_id;
}
} // if(found_id)
} // if(img_class)
// else: try to catch content image id by its url as stored in the database
// find src attribute and catch its value
preg_match( '/<img[^>]*src\s*=\s*[\'"]([^\'"]+)[\'"][^>]*>/i', $img_tag, $img_src );
if ( $img_src ) {
// delete optional query string in img src
$url = preg_replace( '/([^?]+).*/', '\1', $img_src[ 1 ] );
// delete image dimensions data in img file name, just take base name and extension
$guid = preg_replace( '/(.+)-\d+x\d+\.(\w+)/', '\1.\2', $url );
// if path is set relatively make it absolute
if ( 0 === strpos( $guid, '/' ) ) {
$guid = home_url() . $guid;
}
// look up its ID in the db
$found_id = $wpdb->get_var( $wpdb->prepare( "SELECT `ID` FROM $wpdb->posts WHERE `guid` = '%s'", $guid ) );
// if first image id found: return it
if ( $found_id ) {
return absint( $found_id );
} // if(found_id)
} // if(img_src)
} // foreach(img_tag)
} // if(all_img_tags)
// if nothing found: return 0
return 0;
}