Support

Account

Home Forums General Issues Change File url output for CDN

Solving

Change File url output for CDN

  • Hey guys,
    I’m really struggling to figure this one out, I need to alter the output of my file url to use my CDN url (cloudinary) but I can’t manage to achieve it.

    I know that this is similar, but I can’t seem to apply to my situation as I’m not on jetpack.
    https://support.advancedcustomfields.com/forums/topic/use-jetpacks-photon-cdn-for-image-fields/

    My code

    <?php if( get_field(‘video_file_mp4’, $post_id) ): ?><?php the_field(‘video_file_mp4’); ?><?php endif; ?>

    Thanks!

  • There are a couple of choices.

    1) Add an acf/format_value filter https://www.advancedcustomfields.com/resources/acf-format_value/

    
    // filter with high priority to run after ACF
    add_filter('acf/format_value/type=file', 'my_change_to_cdn', 20, 3);
    

    See the filter documentation. In the filter you can replace the domain name of your site with the domain name of the cdn.

    2) Get just the attachment ID and then do all the work of generating the URL yourself using standard WP functions and other code.

    
    // 3rd parameter = false returns unformulated value
    // which is the ID value for images and files
    $attch_id = get_field('file_field_name', false, false);
    
  • Thanks for your help John,

    Unfortunately I haven’t had success as yet with my implementation, am I on the right track here?

    function my_acf_format_value( $value, $post_id, $field ) {
    	
    	$value = str_replace("http://testsite.com.au", "https://d2rfrrfrfr1.cloudfront.net", $value);
    	
    	return $value;
    }
    
    add_filter('acf/format_value/type=file', 'my_acf_format_value', 10, 3);
  • The first thing to try is to increase the priority of your filter to ensure it runs after ACF

    
    add_filter('acf/format_value/type=file', 'my_acf_format_value', 20, 3);
    
  • Thanks John, unfortunately that didn’t do the trick.

  • Are you sure that the value that ACF is passing to your filter contains the domain name? When you look at the html that is created, does the link to the file include the http://testsite.com.au part?

  • 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 ) {
        if(is_array($value)){
            $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);
                }
            }
        }else{
            $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);
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Change File url output for CDN’ is closed to new replies.