Wordpress Snippets

Force WordPress to Select Thumbnail Image Size by Default in WordPress Media Uploader

With WordPress 3.5 release, WordPress core team introduced many significant changes in the core and Media Uploader was one of the most important one. This new WordPress Media Uploader has a number of advantages over the previous version. For instance it has drag and drop support, easier management of galleries, cleaner interface and much more.

The new WordPress Media Uploader is just great and provides tons of features for uploading and editing images but sometimes that is not enough for us. We always look for ways to improve the user experience.

On a project, my client asked me to implement a feature to select a custom thumbnail image size automatically in WordPress Media Uploader when he uploads new images to a post, so he can insert them into content without making few extra clicks.

Lucky for me, WordPress has tons of filters and hooks to help developers alter the default functionality. I used pre_option_(option name) filter to force WordPress to select a custom thumbnail image size by default. This filter is used to temporarily alter a WordPress option during the rendering of a page without changing it permanently in the database.

If you are also looking for a way to auto select a specific custom thumbnail image size in WordPress Media Uploader then you will need to follow these steps.

First make sure your theme has support for post thumbnails and you have already defined the custom thumbnail image size in your functions.php file.

// define custom thumbnail image size
add_theme_support( 'post-thumbnails' );
add_image_size( 'my-image-size', 600, 300, true );

You can change the custom thumbnail image identifier and dimension based on your theme requirement. Now add this following code snippet in your theme’s functions.php file before the closing PHP tag at the bottom.

// force WordPress to select thumbnail image size by default in WordPress media uploader
function wpcs_default_image_size() {
    return 'my-image-size';
}
add_filter( 'pre_option_image_default_size', 'wpcs_default_image_size' );

That’s it, there is nothing more to do. It’s important to note before posting above code snippet that you use same thumbnail image identifier in above code which you defined earlier in your theme, or it will not work.

Hire Us. Or just say Hi!
Need a job? Apply to get one.