Home Forums General Issues Change File url output for CDN Reply To: Change File url output for CDN

  • It might not work because you have set “ID” or object as a returned value, and not URL.

    I had similar issue and this is what I used (note that I restricted this to wp-content/uploads only and it’s for images and files):

    function my_acf_format_value( $value, $post_id, $field ) {
            $value['url'] = str_replace('your-domain/wp-content/uploads', 'cdn/wp-content/uploads', $value['url']);
            if(isset($value['sizes']) && !empty($value['sizes'])){
                foreach($value['sizes'] as $key=>$size){
                    $value['sizes'][$key] = str_replace('your-domain/wp-content/uploads', 'cdn/wp-content/uploads', $size);
            $value = str_replace('your-domain/wp-content/uploads', 'cdn/wp-content/uploads', $value);    
    	return $value;
    add_filter('acf/format_value/type=image', 'my_acf_format_value',10,3);
    add_filter('acf/format_value/type=file', 'my_acf_format_value',10,3);

    And bonus for changing default wordpress URLs for attachments.

    function cdn_attachments_urls($url, $post_id) {
      return str_replace('your-domain/wp-content/uploads', 'cdn/wp-content/uploads', $url);
    add_filter('wp_get_attachment_url', 'cdn_attachments_urls', 10, 2);