WordPress.org

Make WordPress Core

Ticket #10928: remove_thumbnail.diff

File remove_thumbnail.diff, 4.0 KB (added by scribu, 5 years ago)

Add a "Remove Thumbnail" link

  • admin-ajax.php

     
    13951395        if ( !current_user_can( 'edit_post', $post_id ) ) 
    13961396                die( '-1' ); 
    13971397        $thumbnail_id = intval( $_POST['thumbnail_id'] ); 
     1398 
     1399        if ( $thumbnail_id < 0 ) { 
     1400                delete_post_meta( $post_id, '_thumbnail_id' ); 
     1401                die( _wp_post_thumbnail_html() ); 
     1402        } 
     1403 
    13981404        if ( $thumbnail_id && get_post( $thumbnail_id ) ) { 
    13991405                $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' ); 
    14001406                if ( !empty( $thumbnail_html ) ) { 
  • includes/media.php

     
    115115} 
    116116 
    117117function _wp_post_thumbnail_html( $thumbnail_id = NULL ) { 
     118        global $post; 
     119 
     120        $content = '<p class="hide-if-no-js"><a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . esc_html__( 'Set thumbnail' ) . '</a></p>'; 
     121 
    118122        if ( $thumbnail_id && get_post( $thumbnail_id ) ) { 
    119123                $thumbnail_html = wp_get_attachment_image($thumbnail_id, array( 266, 266 ) ); 
    120                 if ( !empty( $thumbnail_html ) ) 
    121                         return apply_filters( 'admin_post_thumbnail_html', '<a href="#" id="cws-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . $thumbnail_html . '</a>' ); 
     124                if ( !empty( $thumbnail_html ) ) { 
     125                        $content = '<a href="#" id="set-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . $thumbnail_html . '</a>'; 
     126                        $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(' . $post->ID .');return false;">' . esc_html__( 'Remove thumbnail' ) . '</a></p>'; 
     127                } 
    122128        } 
    123         return apply_filters( 'admin_post_thumbnail_html', '<a href="#" id="cws-post-thumbnail" onclick="jQuery(\'#add_image\').click();return false;">' . esc_html__( 'Set thumbnail' ) . '</a>' ); 
     129 
     130        return apply_filters( 'admin_post_thumbnail_html', $content ); 
    124131} 
    125132 
    126133function post_thumbnail_meta_box() { 
  • js/set-post-thumbnail-handler.dev.js

     
    11function WPSetThumbnailHTML(html){ 
    22        jQuery('#postthumbnaildiv .inside').html(html); 
    33} 
     4 
    45function WPSetThumbnailID(id){ 
    56        if ( jQuery('input[value=_thumbnail_id]').size() > 0 ) { 
    67                jQuery('#meta\\[' + jQuery('input[value=_thumbnail_id]').attr('id').match(/[0-9]+/) + '\\]\\[value\\]' ).text(id); 
    78        } 
    8 } 
    9  No newline at end of file 
     9} 
     10 
     11function WPRemoveThumbnail(post_id){ 
     12        jQuery.post(ajaxurl, { 
     13                action:"set-post-thumbnail", post_id: post_id, thumbnail_id: -1, cookie: encodeURIComponent(document.cookie) 
     14        }, function(str){ 
     15                if ( str == '0' ) { 
     16                        alert( setPostThumbnailL10n.error ); 
     17                } else { 
     18                        WPSetThumbnailHTML(str); 
     19                } 
     20        } 
     21        ); 
     22} 
  • js/set-post-thumbnail-handler.js

     
    1 function WPSetThumbnailHTML(html){jQuery("#postthumbnaildiv .inside").html(html)}function WPSetThumbnailID(id){if(jQuery("input[value=_thumbnail_id]").size()>0){jQuery("#meta\\["+jQuery("input[value=_thumbnail_id]").attr("id").match(/[0-9]+/)+"\\]\\[value\\]").text(id)}}; 
    2  No newline at end of file 
     1function WPSetThumbnailHTML(html){jQuery("#postthumbnaildiv .inside").html(html)}function WPSetThumbnailID(id){if(jQuery("input[value=_thumbnail_id]").size()>0){jQuery("#meta\\["+jQuery("input[value=_thumbnail_id]").attr("id").match(/[0-9]+/)+"\\]\\[value\\]").text(id)}}function WPRemoveThumbnail(post_id){jQuery.post(ajaxurl,{action:"set-post-thumbnail",post_id:post_id,thumbnail_id:-1,cookie:encodeURIComponent(document.cookie)},function(str){if(str=="0"){alert(setPostThumbnailL10n.error)}else{WPSetThumbnailHTML(str)}})}; 
     2 No newline at end of file