Using WordPress ‘parent_theme_file_uri’ PHP filter

The parent_theme_file_uri WordPress PHP filter allows you to modify the URL to a file in the parent theme.

Usage

add_filter('parent_theme_file_uri', 'your_custom_function', 10, 2);

function your_custom_function($url, $file) {
    // your custom code here
    return $url;
}

Parameters

  • $url (string) – The file URL.
  • $file (string) – The requested file to search for.

More information

See WordPress Developer Resources: parent_theme_file_uri

Examples

Change parent theme image URL

Modify the URL of an image in the parent theme to use a CDN.

add_filter('parent_theme_file_uri', 'use_cdn_for_parent_theme_images', 10, 2);

function use_cdn_for_parent_theme_images($url, $file) {
    if (strpos($file, '.jpg') !== false || strpos($file, '.png') !== false) {
        $url = 'https://your-cdn-url.com' . $file;
    }
    return $url;
}

Add version number to CSS file

Append a version number to a specific CSS file in the parent theme.

add_filter('parent_theme_file_uri', 'add_version_number_to_css', 10, 2);

function add_version_number_to_css($url, $file) {
    if ($file === 'styles.css') {
        $url .= '?ver=1.0.1';
    }
    return $url;
}

Add custom path to parent theme JS files

Modify the URL to JS files in the parent theme to use a custom directory.

add_filter('parent_theme_file_uri', 'custom_js_path_in_parent_theme', 10, 2);

function custom_js_path_in_parent_theme($url, $file) {
    if (strpos($file, '.js') !== false) {
        $url = get_template_directory_uri() . '/custom-js/' . $file;
    }
    return $url;
}

Redirect specific file to external URL

Redirect a specific file in the parent theme to an external URL.

add_filter('parent_theme_file_uri', 'redirect_file_to_external_url', 10, 2);

function redirect_file_to_external_url($url, $file) {
    if ($file === 'sample-file.txt') {
        $url = 'https://external-url.com/sample-file.txt';
    }
    return $url;
}

Add security token to file URL

Add a security token to a specific file URL in the parent theme.

add_filter('parent_theme_file_uri', 'add_security_token_to_file_url', 10, 2);

function add_security_token_to_file_url($url, $file) {
    if ($file === 'secure-file.pdf') {
        $token = 'your-security-token-here';
        $url .= '?token=' . $token;
    }
    return $url;
}