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 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 to add a CNAME from "" to "". This step might be slightly different depending on what CDN you use. Below you can see an example, but we recommend you to contact your CDN provider so they can help you set this up.
    1. On the previous URL, replace "" with your actual domain name.
    2. If "" is already being used on your DNS records, you can change it to whatever you want, like "" or "".
  2. Make sure the new record ( works as expected with an online tool like this one:
  3. Add the following code snippet to your "functions.php" file. You can use a plugin like this one for this.

    function use_my_own_cdn($urlIn) {
    return str_replace("","",$urlIn);

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

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

Example - Cloudflare

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 "" should be changed to your actual domain and that "cdn" should be changed to something else if it's already being used. Make sure the cloud is orange so you can use the CDN features offered by Cloudflare.

Still need help? Contact Us Contact Us