Home › Forums › General Issues › 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);
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);
The topic ‘Change File url output for CDN’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.