diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index 0e524ca24..2a4a2771f 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -1420,13 +1420,8 @@ case 'set-post-thumbnail': die( _wp_post_thumbnail_html() ); } - if ( $thumbnail_id && get_post( $thumbnail_id ) ) { - $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' ); - if ( !empty( $thumbnail_html ) ) { - update_post_meta( $post_ID, '_thumbnail_id', $thumbnail_id ); - die( _wp_post_thumbnail_html( $thumbnail_id ) ); - } - } + if ( set_post_thumbnail( $post_ID, $thumbnail_id ) ) + die( _wp_post_thumbnail_html( $thumbnail_id ) ); die( '0' ); break; case 'date_format' : diff --git a/wp-includes/post.php b/wp-includes/post.php index 02c661d7e..189eae176 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -5257,4 +5257,25 @@ function get_post_format_string( $slug ) { return ( isset( $strings[$slug] ) ) ? $strings[$slug] : ''; } +/** + * Sets a post thumbnail. + * + * @since 3.1.0 + * + * @param int|object $post Post ID or object where thumbnail should be attached. + * @param int $thumbnail_id Thumbnail to attach. + * @return bool True on success, false on failure. + */ +function set_post_thumbnail( $post, $thumbnail_id ) { + $post = get_post( $post ); + if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) { + $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' ); + if ( ! empty( $thumbnail_html ) ) { + update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id ); + return true; + } + } + return false; +} + ?>