The following script will automatically set the first image in a WordPress post as the Featured Image. It will always set the featured image, however, you can select an alternate image.
You could add a checkbox using post meta to bypass this functionality. On this particular site, we preferred to force the blogger to set a featured image. The wrong image only encourages the user to set a better one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
/** * Set the first image in the post content as Featured Image. */ add_action( 'save_post', function( $post_id, $post = NULL ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; if ( !isset( $_REQUEST[ 'post_type' ] ) || $_REQUEST[ 'post_type' ] != 'post' ) return; tsg_set_featured_image( $post ); }, 10, 2 ); add_action( 'transition_post_status', function( $new_status, $old_status, $post ) { if ( $post->post_type != 'post' ) return; tsg_set_featured_image( $post ); }, 10, 3 ); function tsg_set_featured_image( $post ) { if ( $post->post_type != 'post' ) return; if ( has_post_thumbnail( $post ) ) return; $attachment_id = tsg_get_first_attachment_id( $post ); if ( !empty( $attachment_id ) ) update_post_meta( $post->ID, '_thumbnail_id', $attachment_id ); } /** * Get attachment ID from image url. */ function tsg_get_first_attachment_id( $post ) { $image_url = tsg_get_first_image_from_post_content( $post ); if ( empty( $image_url ) ) return; $image_extension = explode( '.', $image_url ); $image_extension = end( $image_extension ); if ( !in_array( $image_extension , [ 'png', 'PNG', 'jpg', 'JPG', 'jpeg', 'JPEG' ] ) ) return; $image_url = parse_url( $image_url ); // Uncomment and add your domain if you only want the database lookup to occur for your domain. // if ( strpos( $image_url[ 'host' ], 'yourdomain.com' ) === false ) // return; $image_path = preg_replace( '/-\d+[Xx]\d+\.' . $image_extension . '/', '.' . $image_extension, $image_url[ 'path' ] ); $attachment_id = tsg_get_post_id_by_guid( $image_path ); return $attachment_id; } /** * Get the first image from the post content. */ function tsg_get_first_image_from_post_content( $post ) { if ( empty( $post->post_content ) ) return; $content = apply_filters( 'the_content', $post->post_content ); preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches ); if ( empty( $matches[ 1 ] ) ) return; return $matches[ 1 ][ 0 ]; } /** * Get post ID by guid. */ function tsg_get_post_id_by_guid( $guid ) { global $wpdb; $post_id = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE instr( guid, '%s' ) > 0", $guid ) ); if ( !empty( $post_id[ 0 ] ) ) return $post_id[ 0 ]; else return; } /** * Describe the new functionality on the Featured Image metabox. */ add_action( 'admin_footer-post-new.php', 'tsg_featured_image_notice' ); add_action( 'admin_footer-post.php', 'tsg_featured_image_notice' ); function tsg_featured_image_notice() { global $current_screen; if ( $current_screen->post_type != 'post' ) return; ?> <script type="text/javascript"> jQuery( '#set-post-thumbnail-desc' ).after( '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc1">The first image in your post is set automatically as the featured image, however, you are able to select an alternate image</p>' ); </script> <?php } |