Support » Plugin: WP SmartCrop » Plugin does not work with custom image sizes in theme

  • Resolved zachary.lv

    (@zacharylv)


    I was testing using this plugin with a custom theme, but could not get the img output to include the wpsmartcrop* classes, or data-smart-crop-focus attributes.

    I dug into the plugin code and added some error logging to monitor what data was being passed around when the_content filter added by the plugin executes.

    From what I’ve seen it appears the plugin won’t work with themes that define custom sizes. WP_Smart_Crop->is_image_size_cropped() returns true if WP_Smart_Crop->get_image_sizes() finds additional images sizes registered in WordPress. When this occurs WP_Smart_Crop->get_smartcrop_focus_attr() returns no data, which means WP_Smart_Crop->make_new_content_img_tag() returns the original img tag unaltered.

    I edited the function WP_Smart_Crop->is_image_size_cropped() to always return false, and then the plugin started working, and I was seeing the focus point data behave as expected. Is there a reason the WP_Smart_Crop->is_image_size_cropped() function exists, and is there some way it can be fixed so this will work with themes that define custom image sizes?

Viewing 8 replies - 1 through 8 (of 8 total)
  • zachary.lv

    (@zacharylv)

    I was doing some additional testing with one of WordPress’ default themes and realized that it’s not custom image sizes that prevent the plugin data from being output, it’s inserting images from an image that uses a hard-crop that prevents output of the data.

    I take it this is on the assumption that images with a hard crop don’t account for extra space necessary to adjust the focal point.

    Turns out I was right the first time. There is an issue with this plugin working with themes and plugins that define custom image sizes. It also prevents the plugin from working when templates make a call for images with the post-thumbnail size defined by WordPress.

    The normal procedure for adding custom images sizes and setting a custom post thumbnail size use add_image_size() which registers the custom size in the $_wp_additional_image_sizes global variable. However, although the WP SmartCrop loads this variable when it checks for available image sizes, it does not use the values stored in this variable to confirm if an image should output the wpsmartcrop classes and data attribute.

    Instead the plugin pulls the dimensions and crop value for registered image sizes from the options table in the database on lines 598-602 in wp-smartcrop.php. These options only exist for the default image sizes in WordPress (thumbnail, medium, medium-large, large). Because of this SmartCrop will not work for any custom image sizes, or for the post-thumbnail size defined in WordPress, as that also does not record dimensions in the database.

    I was able to solve this problem on my custom theme by calling update_option( $my_size . ‘_size_w’, $width ); etc for all my custom defined image sizes after adding them with add_image_size() and for the post-thumbnail size. This makes the option data available when SmartCrop tries to verify an image size will support a focus point. This data is irrelevant to WordPress core and my custom theme, however, so it’s just a bandaid in a sense.

    The plugin should be updated so it can work properly with custom image sizes.

    A bit of a headache sorting this out, but otherwise a cool plugin when it’s working.

    Plugin Author Bytes.co

    (@burlingtonbytes)

    Hello zachary.lv,

    We’ve updated the plugin to version 2.0.2, which should resolve this issue.

    Let us know if you continue to experience any difficulties.

    Best,
    Bytes.co

    Ignore this

    @zacharylv Did the 2.0.2 version resolve your issue? I just ran into this issue on a site and am trying to find a solution. 2.0.2 doesn’t fix it for me.

    I’m pulling in an image with wp_get_attachment_image — WP Smartcrop works fine if the function calls for default “medium” size, but if I switch to a custom size, WP Smartcrop doesn’t run on it. 🤔

    @burlingtonbytes Do you have any ideas regarding the above message? I would be ever so grateful to get this working. My last bug on a site build! haha

    • This reply was modified 7 months, 3 weeks ago by Rachelle Wise.
    Plugin Author Bytes.co

    (@burlingtonbytes)

    Hi @chellechan,

    We’re still working on fix for this. Earlier in this thread @zacharylv documented a workaround for the issue, by calling update_option to register your custom sizes in wp_options. I’d recommend giving that a shot until we can get a patch for this tested and released.

    Thank you @zacharylv for the work you put into troubleshooting this!

    Ah! Totally overlooked that. Thank you, @burlingtonbytes. Will do. And thanks so much for creating this great plugin and keeping it up — discovering it has been a major game-changer for me. 🙌

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Plugin does not work with custom image sizes in theme’ is closed to new replies.