How to check if a custom image size exists? (5 posts)

  1. Ionut
    Posted 3 years ago #

    Is there a way to check if a custom image size exists?

    I use a custom size image "large" in a plugin, but when it's not available I want it to fall back to the original file size or a thumbnail. How can we do this?

  2. alexleonard
    Posted 3 years ago #

    Let's assume your large image width is 800px.
    If you do something like this it should work:

    $largeImage = wp_get_attachment_image_src($post->ID, 'large');
    // check if the largeImage width is equal to your setting of 800px
    if ($largeImage[1] == 800) {
      // if it is output large size
    } else {
      // if it isn't then show the full size image

    I haven't tested this code, but it should work.

  3. mwolstat
    Posted 3 years ago #

    I have this same problem, but the above solution is no good. I want to be able to test if a any image size handle exists without having to know its dimensions. i.e.

    does_image_size_exist( 'large')
    does_image_size_exist( 'sm_movie_poster')
    does_image_size_exist( 'sidebar_avatar')

    if the size hasn't been added with add_image_size() there are some functions I need to call before outputting the image.

  4. alexleonard
    Posted 2 years ago #

    I haven't tested this, but you could experiment with:



  5. mwolstat
    Posted 2 years ago #

    Thanks, I'll look into that!

Topic Closed

This topic has been closed to new replies.

About this Topic