Adding a custom image upload size and making it selected by default?

How can I add a custom image upload size with a width of 550px and make it selected by default in the Add Media page?

Currently, there are 4 sizes available: Thumbnail, Medium, Large and Full Size. The Full Size option is selected by default.

Read More

Anyone knows a working solution to this?

Related posts

Leave a Reply

2 comments

  1. There are a few steps to this, as I found out today. First, you add a custom image size (you can put this somewhere the functions.php file in your theme):

    add_theme_support( 'post-thumbnails' );
    add_image_size( 'my-size', 550, 550 ); // not sure what you want your height to
                                         // be, but this'll shrink it to 550px
                                         // wide *or* tall. If you want it to be
                                         // cropped to a 550px square, add true as
                                         // a fourth argument
    

    The unfortunate thing is that WordPress doesn’t actually show this new size in the media uploader. You’ll have to put a function that does this into your functions.php as well (source: http://kucrut.org/insert-image-with-custom-size-into-post/):

    function my_insert_custom_image_sizes( $sizes ) {
        // get the custom image sizes
        global $_wp_additional_image_sizes;
        // if there are none, just return the built-in sizes
        if ( empty( $_wp_additional_image_sizes ) )
            return $sizes;
    
        // add all the custom sizes to the built-in sizes
        foreach ( $_wp_additional_image_sizes as $id => $data ) {
            // take the size ID (e.g., 'my-name'), replace hyphens with spaces,
            // and capitalise the first letter of each word
            if ( !isset($sizes[$id]) )
                $sizes[$id] = ucfirst( str_replace( '-', ' ', $id ) );
        }
    
        return $sizes;
    }
    
    // apply the above function as a filter on the media uploader's list of
    // image sizes
    add_filter( 'image_size_names_choose', 'my_insert_custom_image_sizes' );
    

    Lastly, to set this size as the default, you’ll need to change a WordPress setting. Add a filter on the setting in question. This has the benefit of being portable with your theme in which you’ve defined all the above options.

    function my_set_default_image_size () {
        return 'my-size';
    }
    add_filter( 'pre_option_image_default_size', 'my_set_default_image_size' );
    

    I tested this all out on WordPress 3.4.2, and it seems to work great.

    Note: I’d recommend putting the above PHP code into an init function of some sort and attaching it to the 'after_setup_theme' hook:

    // define the functions first
    function my_insert_custom_image_sizes( $sizes ) {
        // not going to repeat the function body, for brevity's sake
    }
    
    function my_set_default_image_size() {
        // ditto
    }
    
    // define the init function next, which sets up all the necessary stuff
    function custom_image_setup () {
        add_theme_support( 'post-thumbnails' );
        add_image_size( 'my-size', 550, 550 );
        add_filter( 'image_size_names_choose', 'my_insert_custom_image_sizes' );
        add_filter( 'pre_option_image_default_size', 'my_set_default_image_size' );
    }
    
    // and attach that init function to the 'after_setup_theme' hook
    // so it runs on each page load once your theme's been loaded
    add_action( 'after_setup_theme', 'custom_image_setup' );