5

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.

Anyone knows a working solution to this?

2 Answers 2

7

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' );
3
  • No need for the SQL, you can use the pre_option_{$option_name} filter. Eg. add_filter('pre_option_image_default_size', function() { return 'my-size'; }); Nov 21, 2012 at 1:04
  • @chrisguitarguy: thanks for the tip; I didn't know about that and have updated my answer accordingly. This has the added benefit of being more portable. Nov 26, 2012 at 19:15
  • there is no function needed to set the default image size to a custom value: update_option('image_default_size', 'my-size' ); does the trick. but this only works with your my_insert_custom_image_sizes function, so thanks for that :)
    – honk31
    Jun 23, 2014 at 9:31
0

Just set all other options to 0 in your Admin panel under "Settings" » "Media". This will disable them.

6
  • This did not answer my question, unless if I am confused. I asked: 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? Sep 23, 2012 at 22:29
  • Add a custom image size (see Codex) and then set all default ones to 0. That does answer your question.
    – kaiser
    Sep 23, 2012 at 22:48
  • No but then the Full Size one will be selected by default. As I am sure that you already know, I cannot remove Full Size from the list and I do not want to either. How can I make so that my custom sized one is selected by default and not Full Size? Sep 23, 2012 at 22:52
  • Ah, ok. I understand your needs.
    – kaiser
    Sep 23, 2012 at 23:14
  • Do you know a solution to it buddy? Sep 23, 2012 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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