0

When uploading new (or existing) images to the media library in Wordpress 3.6.1 the preview generated on the admin page/edit media page (eg. wp-admin/post.php?post=8736&action=edit ) is showing one of my custom image sizes defined from my functions file, rather than the default size.

I am using:

add_image_size( 'narrow', 205, 450, true );

In functions.php with after_setup_theme

This is an issue because this size is for part of the theme and is cropped so does not show the image as intended.

Question: Why is Wordpress choosing a custom image size on the editor and how can I force it to use a standard image size such as 'medium'.

Thanks

2
  • By chance are you doing anything that disables default image sizes or using plugin/theme that might do that as part of its functionality? Are standard sizes actually generated and available (as in files on disk) for your images?
    – Rarst
    Sep 24, 2013 at 20:03
  • No, not using any plugins at the moment. It is generating all the other images fine, I use Large, Medium and this Custom 'Narrow' size through the theme.
    – Alex
    Sep 25, 2013 at 16:56

2 Answers 2

1

This is less a fix, and more of a hack to possibly achieve what you need.

In my case, WP was using the largest possible image size as the preview thumbnail in the Media Library.

As noted in other answers, this is happening in a JS template that relies on data.size.url which I was unable to ensure got the correct image information.

As noted, print_media_templates does not have a filter but I was able to use add_action. In my case it runs after the markup is in the page, and it modies the JS template by doing a search and replace in the innerText.

It happens soon enough that this is what WP then relies on.

The advantage is that you do not have to maintain a separate copy of media-template.php with WordPress Core. You will need to check from time to the that this particular JS/template code doesn't change - so risks are minimized but not zero.

function modify_media_library_js_template(){
    ?>
    <script>
    (function(){
        var el = document.getElementById('tmpl-attachment') || false
        if (el) {
            el.innerHTML = el.innerText.replace(/\{\{\s*data\.size\.url\s*\}\}/g, "{{ data.sizes['0200-01-X'].url }}");
        }
    })();
    </script>
    <?php
}
add_action( 'print_media_templates', 'modify_media_library_js_template', 1 );
0

It's actually a feature, not a bug! :)

WordPress will use (actually image_get_intermediate_size() function) a image already cropped if the width or the height matches the required dimension, and the required dimenssion for image editor is 900x450, EXACTLY your defined width.

A workaround will be to adjust your custom image size to 205 x 499 (a pixel smaller is enough)

OR to make the default medium size 450 by 450 ( not required to be square ).

2
  • Hi, I cannot change either Medium or the Custom 'Narrow' image size, i'm using them in my theme for exact dimensions. Is there any filter which can alter the media library image preview size?
    – Alex
    Sep 25, 2013 at 16:57
  • Nope, and I told you the problem is the way WordPress chooses the 'right' image for a given dimension. Sep 25, 2013 at 20:38

Your Answer

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

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