Can I use a different CDN with Autoptimize?

Important note: Autoptimize works correctly out-of-the-box, so configuring it to work with your own CDN should only be done if you have a good technical knowledge.

If you're interested in using a different CDN when serving optimized images with  Autoptimize, then please:

  1. Edit your DNS records and add a CNAME from "cdn.your-domain.com" to "no-cdn.shortpixel.ai"
    1. On the previous URL, replace "your-domain.com" with your actual domain name.
    2. If "cdn.your-domain.com" is already being used on your DNS records, you can change it for whatever you want, like "images.your-domain.com" or "img.your-domain.com".
  2. Add the following code snippet to your functions.php file. You can use a plugin like this one for this.


    add_filter("autoptimize_filter_imgopt_base_url","use_my_own_cdn");
    function use_my_own_cdn($urlIn) {
    return str_replace("cdn.shortpixel.ai"," cdn.example.com",$urlIn);
    }

    Remember that you will need to change " cdn.example.com" to the new URL you just created, replacing "example.com" with your domain and "cdn" with the CNAME you chose.

Note: the filter name changed since version 2.5 of Autoptimize.

Example

Here is a step-by-step example on how to set up the CNAME (previous step 1) on Cloudflare. Please notice that we are not responsible for any changes made by Cloudflare that would invalidate the following steps. Therefore, if you encounter any issues while creating your new CNAME in your DNS records, please contact your CDN or hosting provider.

  1. Enter your Cloudflare account
  2. Select the DNS tag from the top menu.

  3. Add a new record following this example. 

    Remember that "your-domain.com" should be changed to your actual domain. Make sure the cloud is yellow so you can use the CDN features offered by Cloudflare.

Important: after setting up your own CDN make sure the new record works as expected with an online tool like this one https://www.whatsmydns.net/

Still need help? Contact Us Contact Us