Rewriting a core function?

You don’t need to override a core function for that, what you need to do is to hook on the admin_post_thumbnail_html filter.

add_filter('admin_post_thumbnail_html', 'wpse107096_wp_post_thumbnail_html',10, 2);

function wpse107096_wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
    global $content_width, $_wp_additional_image_sizes;

    $post = get_post( $post );

    $upload_iframe_src = esc_url( get_upload_iframe_src('image', $post->ID ) );
    $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="https://wordpress.stackexchange.com/questions/107096/%s" id="set-post-thumbnail" class="thickbox">%s</a></p>';
    $content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) );

    if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
        $old_content_width = $content_width;
        $content_width = 700;
        if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
            $thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
        else
            $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
        if ( !empty( $thumbnail_html ) ) {
            $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID );
            $content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
            $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>';
        }
        $content_width = $old_content_width;
    }

    return $content;
}

yes, you will waste some CPU cycles because of the repeated computation, but that is the proper way to do it. If it bothers you then you should look for a smarter way to set the proper required content size.

Leave a Comment