The content_url WordPress PHP filter allows you to modify the URL to the content directory.
Usage
add_filter('content_url', 'your_custom_function', 10, 2);
function your_custom_function($url, $path) {
// your custom code here
return $url;
}
Parameters
$url(string) – The complete URL to the content directory including scheme and path.$path(string) – Path relative to the URL to the content directory. Blank string if no path is specified.
More information
See WordPress Developer Resources: content_url
Examples
Change content URL to use a CDN
Change the content URL to use a CDN for faster content delivery.
add_filter('content_url', 'use_cdn_for_content', 10, 2);
function use_cdn_for_content($url, $path) {
$cdn_url = 'https://your_cdn_url.com';
return $cdn_url . $path;
}
Add a subdirectory to content URL
Add a subdirectory to the content URL.
add_filter('content_url', 'add_subdirectory_to_content', 10, 2);
function add_subdirectory_to_content($url, $path) {
$subdir = '/subdir';
return $url . $subdir . $path;
}
Change content URL protocol to HTTPS
Force the content URL to use HTTPS.
add_filter('content_url', 'force_https_content', 10, 2);
function force_https_content($url, $path) {
$url = set_url_scheme($url, 'https');
return $url . $path;
}
Add a query string to content URL
Add a query string to the content URL for cache busting.
add_filter('content_url', 'add_query_string_to_content', 10, 2);
function add_query_string_to_content($url, $path) {
$query_string = '?v=123';
return $url . $path . $query_string;
}
Replace domain in content URL
Replace the domain in the content URL for migration purposes.
add_filter('content_url', 'replace_domain_in_content', 10, 2);
function replace_domain_in_content($url, $path) {
$old_domain = 'https://old-domain.com';
$new_domain = 'https://new-domain.com';
$new_url = str_replace($old_domain, $new_domain, $url);
return $new_url . $path;
}