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 ) {
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);
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.