• Resolved Rodrigo Gomes

    (@rodrigogomes-1)


    Hello guys,

    I wrote the script below to replace the URL of the images in the post content with the WordPress CDN link. (Yes, I know the Jetpack, but I want to use CDN without needing Jetpack.)

    <?php
    add_filter('the_content','cdn_imageurl',9999);
    function cdn_imageurl($content) {
        // Prepare url for regex
        $url = str_replace("/", "\/", get_site_url());
        $url = str_replace(".", "\.", $url);
    
        // Find the images that matches the boundaries of the CDN
        $content = preg_replace_callback("@(?i)<img.+?(src=[\"'](".$url."((?![\"']).)*\/wp-content\/uploads\/((?![\"']).)*\.(gif|jpe?g|png))[\"']).+?>@",'cdn_imageurl_replace',$content);
        return $content;
    }
    
    function cdn_imageurl_replace($matches) {
        // Do not proceed if src is empty
        if(!$matches[2]) { return $matches[0]; }
    
        // Prepare url for regex
        $url = str_replace("/", "\/", get_site_url());
        $url = str_replace(".", "\.", $url);
    
        // Find the images that matches the boundaries of the CDN (child)
        $matches = preg_replace_callback("@(?i)((src|data-orig-file|data-medium-file|data-large-file|srcset)=[\"'](".$url."((?![\"']).)*\/wp-content\/uploads\/((?![\"']).)*)[\"'])@",'cdn_imageurl_replace_child',$matches);
        return $matches[0];
    }
    
    function cdn_imageurl_replace_child($matches) {
        // Do not proceed if src is empty
        if(!$matches[3]) { return $matches[0]; }
    
        // Generates cdn subdomain number
        $url = parse_url(get_site_url());
        $url = str_replace("www.", "", $url['host']);
        srand(crc32(basename($url)));
        $static_rand = rand(0,2);
        srand(); // this resets everything that relies on this, like array_rand() and shuffle()
    
        // Replaces the protocol with cdn url
        $wp = '//i'.$static_rand.'.wp.com/';
        $url_replace = preg_replace("/(http:\/\/|https:\/\/)/i", $wp, $matches[3]);
        $replace = str_replace($matches[3],$url_replace,$matches[0]);
        return $replace;
    }

    It works great for the_widget filter. However, how can I adapt my code to also replace the images urls in the widgets and post thumbnail?

    I’ve tried to do this in countless ways. I tried to use the the_widget and the_post_thumbnail filters, but without success.

    I was able to do this using the pre_option_upload_url_path filter. However, with this filter I can not identify the type of image. And I need to check this out because the WordPress CDN only accepts some types of images.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You must use a filter that passes HTML output in order to be able to do what you want. “the_widget” is an action and is thus unsuitable. You would need a widget specific filter offered in its widget() class method.

    For featured images, you can use ‘post_thumbnail_html’ filter. Your callback is passed the attachment ID (3rd param), from which you can learn its MIME type by getting its metadata from postmeta, key “_wp_attachment_metadata”.

    Thread Starter Rodrigo Gomes

    (@rodrigogomes-1)

    Hello @bcworkz

    I was able to do what I wanted with your support, thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How do I replace the images URL in widgets?’ is closed to new replies.