Support » Plugin: WP Super Cache » CDN and product variation images issue

  • Hello all!

    There seems to be a problem with the CDN feature with woocommerce. It cannot rewrite the url of the images of the .jpg files on “data-product_variations” attribute of the form “variations_form. After viewing the source from my browser the output goes like:

    data-product_variations=”[{":"https:\/\/\/storefront\/wp-content\/uploads\/sites\/78\/2015\/07\/stripy-top.jpg","……. should have been

    Issue maybe related to that. So now it loads the product images multiple times, once from my server and the CDN for the other location of the product images.

    Have also searched for solution and it seems this problem has already been existing as well with other caching and cdn plugin. And according to Woocommerce team it is an issue with the caching plugin:

    Also the wp fastest cache team seems to have fixed it:

    Not verified though as not using it, maybe as a last resort. Very happy with wp supercache for now and really hope this gets fixed as it could stop woocommerce users from using it and also causes them this issue which defeats the purpose of speed as it loads the product images multiple times.

Viewing 5 replies - 1 through 5 (of 5 total)
  • WP Super Cache (and probably other cache plugins) uses regular expressions to replace URLs in output buffers. I think that current “pattern” doesn’t match URLs in JSON escaped strings.

    There are couple possibilities:
    – Improve regular expressions.
    – Use WP filter wp_get_attachment_url.

    I think that I could write PHP snippet which you can add in functions.php. Let me know if you are interesting for this solution.

    I’m working on PR – Fixes regex and optimizes ossdl-cdn.php. Maybe it’s possible to add something like this if we find out more details.

    Yes please a snippet to temporarily patch this would be really great!

    I’ve created workaround which uses WPSC function and it works only for function woocommerce_variable_add_to_cart. I’d guess that “CDN rewriting” is already set. For everyone which will try this – this snippet doesn’t check CDN options (because I wanted to simplify this snippet). Please make sure that CDN is working correctly before apply this snippet.

    if ( ! is_admin() && function_exists( 'scossdl_off_rewriter' ) ) {
            add_action( 'woocommerce_variable_add_to_cart', 'start_my_cdn_filter' );
            add_action( 'woocommerce_variable_add_to_cart', 'stop_my_cdn_filter', 100 );
            function start_my_cdn_filter() {
                    add_filter( 'wp_get_attachment_url', 'my_cdn_rewrite_filter' );
                    add_filter( 'wp_get_attachment_image_src', 'my_cdn_rewrite_filter' );
            function stop_my_cdn_filter() {
                    remove_filter( 'wp_get_attachment_url', 'my_cdn_rewrite_filter' );
                    remove_filter( 'wp_get_attachment_image_src', 'my_cdn_rewrite_filter' );
            function my_cdn_rewrite_filter( $url ) {
                    if ( is_array( $url ) ) {
    			$matches[0] = $matches[1] = $url[0];
    			$url[0] = scossdl_off_rewriter( $matches );
                    } else {
    			$matches[0] = $matches[1] = $url;
    			$url = scossdl_off_rewriter( $matches );
                    return $url;

    I’m still working on this issue and I’ll create new issue on github when I find out more details about possible solutions.

    Unfortunately its not working the datas on data-product_variations are still not being overwritten and still on www. On the page load though there is no longer a call for the www. version but when changing the variation the call to the www. version of the image happens.

    Previous snippet has worked in my tests. Anyway, I’m trying to find way to use regular expressions and I think that I’ve solution which could handle all JSON encoded URLs. I’ve tested in on development server and it works.

    add_filter( 'wp_cache_ob_callback_filter', 'my_json_urls_ob_filter', 20 );
    function my_json_urls_ob_filter( $content ) {
            global $ossdl_off_blog_url, $ossdl_off_cdn_url;
            if ( empty( $content ) || empty( $ossdl_off_cdn_url ) || $ossdl_off_blog_url === $ossdl_off_cdn_url ) {
                    return $content; // no rewrite needed.
            $old_ossdl_off_blog_url = $ossdl_off_blog_url;
            $old_ossdl_off_cdn_url  = $ossdl_off_cdn_url;
            $ossdl_off_blog_url     = addcslashes( $ossdl_off_blog_url, '/' );
            $ossdl_off_cdn_url      = addcslashes( $ossdl_off_cdn_url, '/' );
            $dirs    = scossdl_off_additional_directories();
            $regex   = '#(?<=[\s\"\';])' . preg_quote( $ossdl_off_blog_url . '\/', '#' ) . '(?:((?:' . $dirs . ')[^\s\"\'&)]+)|([^/\s\"\'&]+\.[^/\s\"\'&)]+))(?=[\s\"\'&)])#';
            $content = preg_replace_callback( $regex, 'scossdl_off_rewriter', $content );
            $ossdl_off_blog_url = $old_ossdl_off_blog_url;
            $ossdl_off_cdn_url  = $old_ossdl_off_cdn_url;
            return $content;

    If it works for your website then I’ll try to integrate it into PR on github.

    • This reply was modified 1 year, 11 months ago by Jan Dembowski.
    • This reply was modified 1 year, 11 months ago by Saša. Reason: Impossible to add ` into "code snippet"
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘CDN and product variation images issue’ is closed to new replies.