Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

As I understand it, it's possible to overwrite core functions in wordpress, without touching the core itself, which is of obvious benefit.

Now, certain functions are 'pluggable', but not all of them.

I'm trying to overwrite the function that outputs the featured image thumbnail in the admin panel to be a custom size, it's not really difficult to do this in the core. The problem arises when I try to put it in my functions.php as a renamed function.

This is the core function, edited. What I'd do is just rename it and put it in functions.php, but then how do I actually have it overwrite the 'core' function itself?

function _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="%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 apply_filters( 'admin_post_thumbnail_html', $content, $post->ID );
}
share|improve this question
    
The easiest hack would be to copy that function in your functions.php, rename it, modify as needed, and then use it instead. –  RRikesh Jul 19 '13 at 5:22
    
I done that, when I rename it and put it in functions.php it doesn't override the core function. –  andy Jul 19 '13 at 5:23
    
It will not override, it will just be a "new" function that you can use instead of _wp_post_thumbnail_html. –  RRikesh Jul 19 '13 at 5:26
    
And how would I go about using the new function instead of _wp_post_thumbnail_html? Sorry if that's a silly question. –  andy Jul 19 '13 at 5:37
    
I have no idea what you're trying to do. if you're calling the function yourself, you can call the new function. Else you can write a function to filter the output using the admin_post_thumbnail_html filter. –  RRikesh Jul 19 '13 at 6:34
add comment

2 Answers

Only the functions defined as "pluggable" functions can be overridden in a plugin/theme.

But... You shouldn't need to rewrite a core function to resize your featured images. See instead set_post_thumbnail_size() (and, more generally, add_image_size()).

share|improve this answer
    
Resize featured images IN the admin panel, not on the front end of the site, which is easy. –  andy Jul 19 '13 at 4:50
    
So what exactly do you want to resize in the Admin panel? The thumbnail of the image that is displayed? Or are you looking to have control over how large the Featured Image is once it's attached to a post/page? –  Pat J Jul 19 '13 at 13:48
    
Yes, the thumbnail image that's displayed in the admin panel. So far I can't seem to get the filter to work properly so the only way is to over write the core function. –  andy Jul 19 '13 at 15:47
    
What filter(s) have you tried? You can check on the full list of filters at sites like wpseek.com or adambrown.info/p/wp_hooks -- they're not all listed in the Codex, not by a long shot. –  Pat J Jul 19 '13 at 15:50
    
Only admin_post_thumbnail_html as per mark's answer, I have no idea which other ones to even try. –  andy Jul 19 '13 at 21:05
add comment

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="%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.

share|improve this answer
    
This fails to display the thumbnail for some reason. –  andy Jul 19 '13 at 9:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.