Make WordPress Core


Ignore:
Timestamp:
07/20/2016 04:23:36 PM (9 years ago)
Author:
joemcgill
Message:

Post Thumbnails: Only update featured images when saving a post.

Previously, changing the post thumbnail of a published post in the edit screen
would immediately apply the change, rather than waiting for the post to be
saved before applying the update. This could lead to someone unintentionally
editing the post thumbnail on a published post, and made it impossible to
preview changes to post thumbnails on published posts before saving the change.

This introduces a new Ajax handler, wp_ajax_get_post_thumbnail_html() to
retrieve the HTML for the post thumbnail meta box without updating the post
meta value for _thumbnail_id. It also allows post thumbnail changes to be
previewed by passing the _thumbnail_id as a query variable to the preview
screen and adding a new filter, _wp_preview_post_thumbnail_filter(), which
gets applied to get_post_metadata during the post preview process.

Props flixos90.
Fixes #12922.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r38091 r38118  
    21652165
    21662166/**
     2167 * Ajax handler for retrieving HTML for the featured image.
     2168 *
     2169 * @since 4.6.0
     2170 */
     2171function wp_ajax_get_post_thumbnail_html() {
     2172    $post_ID = intval( $_POST['post_id'] );
     2173
     2174    check_ajax_referer( "update-post_$post_ID" );
     2175
     2176    if ( ! current_user_can( 'edit_post', $post_ID ) ) {
     2177        wp_die( -1 );
     2178    }
     2179
     2180    $thumbnail_id = intval( $_POST['thumbnail_id'] );
     2181
     2182    // For backward compatibility, -1 refers to no featured image.
     2183    if ( -1 === $thumbnail_id ) {
     2184        $thumbnail_id = null;
     2185    }
     2186
     2187    $return = _wp_post_thumbnail_html( $thumbnail_id, $post_ID );
     2188    wp_send_json_success( $return );
     2189}
     2190
     2191/**
    21672192 * Ajax handler for setting the featured image for an attachment.
    21682193 *
Note: See TracChangeset for help on using the changeset viewer.