Support

Account

Home Forums General Issues Strategy for Custom Image Sizes Reply To: Strategy for Custom Image Sizes

  • Is it possible to create a custom image size only for images uploaded to a specific ACF field. No, not really.

    It is possible to create a custom image size that is only applied to specific images. The answer to this is yes, but it is only possible if you build your own template code and you use the WP function wp_get_attachment_image_src() or some other function in WP that implements this function like wp_get_attachment_image_url().

    This image size will not appear anywhere in WP, it would only be available in code to those that knows that the image size exists. This could not be used in any type of page builder or shown in any WP admin UI including the ACF UI when editing a page.

    I don’t have code that I can supply, but the following is then general idea.

    This would involve adding a filter on the WP Hook ‘image_downsize’ which is called by the WP function image_downsize(). If you return something other than false from your this filter what you return will be returned by wp_get_attachment_image_src() so you must return the same array that you would expect when calling this function.

    In the filter you would need use wp_get_attachment_metadata() and check this data to see if the image of the size already exists.

    If it does not then you would generate a new image field using image_make_intermediate_size(), add the size to the image data and update the image data using wp_update_attachment_metadata(). After doing this you would return the image array as explained in the return value of wp_get_attachment_image_src().

    Generally, the new image would be created the first time wp_get_attachment_image_src() is called for the image. It would be possible to do this when the post is saved in an acf/save_post filter.