12

Bare with me. I want to have a custom image size selected by default in the Media upload popup page. In Wordpress v3.4.2 and prior, this elegant code worked fine:

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;
}


// Which custom image size selected by default
function my_set_default_image_size () { 
     return 'custom-image-size-2';
}


function custom_image_setup () {
    add_theme_support( 'post-thumbnails' );
    add_image_size( 'custom-image-size-1', 160, 9999 ); //  columned
    add_image_size( 'custom-image-size-2', 300, 9999 ); //  medium
    add_image_size( 'custom-image-size-3', 578, 190, true ); //  cropped
    add_filter( 'image_size_names_choose', 'my_insert_custom_image_sizes' );
    add_filter( 'pre_option_image_default_size', 'my_set_default_image_size' );
}

add_action( 'after_setup_theme', 'custom_image_setup' );

So, my_insert_custom_image_sizes adds the custom images to the media page and my_set_default_image_size should select the custom-image-size-2 size. This code stopped working with the Wordpress 3.5 version. Do you know how I can accomplish this in v3.5?

1

2 Answers 2

2

Try this out. Your add_filter()'s second argument is a function, that will affect the current option via a return:

function theme_default_image_size() {
    return 'custom-image-size-2';
}
add_filter( 'pre_option_image_default_size', 'theme_default_image_size' );

You could also look into the pre_update_option_{$option} filter, and update the value once, so you don't need to run this filter every time (may save 0.01s, but it's still saving!) :)

or good old update_option():

update_option( 'image_default_size', 'custom-image-size-2' );
0

Add the function to theme's functions.php file.

if ( function_exists( 'add_theme_support' ) ) {
    add_theme_support( 'post-thumbnails' );
        set_post_thumbnail_size( 150, 150 ); // default Post Thumbnail dimensions   
}


function custom_image_setup () {

        add_theme_support('post-thumbnails');
        set_post_thumbnail_size(640,320);

    add_image_size( 'custom-image-size-1', 180, 9999 ); //  columned
    add_image_size( 'custom-image-size-2', 350, 9999 ); //  medium
    add_image_size( 'custom-image-size-3', 600, 250, true ); //  cropped

    add_filter( 'image_size_names_choose', 'theme_custom_image_sizes' );
    add_filter( 'pre_option_image_default_size', 'theme_default_image_size' );
}


if ( function_exists( 'add_image_size' ) ) { 
    add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)
    add_image_size( 'homepage-thumb', 220, 180, true ); //(cropped)
}

Using the New Image Sizes Within a theme's template files.

if ( has_post_thumbnail() ) { the_post_thumbnail( 'category-thumb' ); }

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.