WordPress.org

Ready to get started?Download WordPress

Forums

image_size_names_choose not working (7 posts)

  1. Topher
    Member
    Posted 2 years ago #

    I'm trying to get the Image Uploader to show my cutom image sizes in the list. Here's my code:

    function custom_wmu_image_sizes($sizes) {
            $myimgsizes = array(
                    "vnn-slide-cropped" => __( "VNN Slide" )
                    );
            $newimgsizes = array_merge($sizes, $myimgsizes);
    
            return $newimgsizes;
    }
    add_filter('image_size_names_choose', 'custom_wmu_image_sizes');

    When I do a print_r on $newimgsizes I can see that me new sizes are actually in there, but they don't render on the page, which makes me think my filter isn't working properly. I don't know how to debug that though. :(

    Where do I go from here?

  2. Topher
    Member
    Posted 2 years ago #

    I figured it out. I had previously altered $sizes in another function that ran AFTER this one, killing my changes. All set now.

  3. Luke Rumley
    Member
    Posted 2 years ago #

    Dude, does this code put your custom images sizes into the standard WordPress upload/insert dialog?? That rocks. So the code snippet above is good as long as I can create the array of my sizes, and obviously VNN is your prefix...?

  4. Topher
    Member
    Posted 2 years ago #

    Yep, that's exactly what it does.

  5. myerdogan
    Member
    Posted 2 years ago #

    Than

    foreach( $added_sizes as $key => $value) {
    		$new_sizes[$value] = $value;
    	}
  6. memelab
    Member
    Posted 1 year ago #

    Here's my version, which grabs all the custom defined sizes and formats them prettily:

    add_filter( 'image_size_names_choose', 'ml_custom_image_choose' );
    function ml_custom_image_choose( $args ) {
    
    	global $_wp_additional_image_sizes;
    
    	// make the names human friendly by removing dashes and capitalising
    	foreach( $_wp_additional_image_sizes as $key => $value ) {
    		$custom[ $key ] = ucwords( str_replace( '-', ' ', $key ) );
    	}
    
    	return array_merge( $args, $custom );
    }
  7. Luke Rumley
    Member
    Posted 1 year ago #

    Good tip, @memelab! I googled that $_wp_additional_images_sizes var as I hadn't seen it before and found an article recommending the use of get_intermediate_image_sizes() instead (http://www.deluxeblogtips.com/2011/06/list-registered-image-sizes.html). Good stuff!

Topic Closed

This topic has been closed to new replies.

About this Topic