WordPress new image size not showing correctly

I am trying to add a new image size and have it selectable in the media modal.

I have checked that a new, correct sized image is being created ie 670px wide

Read More

But the modal window is showing it as being 604px wide and when inserted you get:

<img width="604" height="453" alt="two-new-honeys" src=".../two-new-honeys-670x503.jpg" class="..">

IE its putting in the right image, but adding in wrong width and height values in the html

What can I do to get the right width / height

In functions.php I have added:

// add new image size(s)
add_theme_support( 'post-thumbnails' );

add_action( 'after_setup_theme', 'bsb_add_image_size' );
function bsb_add_image_size() {
  add_image_size( '2-column', 670 ); // 670 pixels wide (and unlimited height)
}


add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
    return array_merge( $sizes, array(
        '2-column' => __( '2 Column width' ),
    ) );
}

Related posts

2 comments

  1. This was down to a theme specific variable called $content_width that was set for the theme 2013 at 604px – therefore the media modal was overriding my image at 670px and scaling it down the the size set by $content_width.

    Solution is to reset $content_width=xxx within functions.php

    See https://codex.wordpress.org/Content_Width

  2. In case someone might need it – there is a way to specify $content_width by template name. Code have to be placed in your functions.php

    <?php
    
    //adjust $content_width only for chosen template
    function custom_adjust_content_width() {
        global $content_width;
    
        if ( is_page_template( 'my-template.php' ) )
            $content_width = 640;
    }
    add_action( 'template_redirect', 'custom_adjust_content_width' );
    ?>
    

Comments are closed.