Exclude external uri for css and js version

I use the following code to add version ?ver=xxx based on file last modified date to css and js. It works fine, but when I use external assets, it runs into errors, as get_theme_file_uri(), and get_theme_file_path() won’t work anymore.


Is it possible to fix that? Perhaps not to have the ?ver or use the WP default versioning system ver=4.8 … for remote assets instead.

Other than that, can the code below be simplified slightly if possible?

function _enqueue_scripts() {

    function style($handle, $file, $deps=array(), $media='all') {
        $src = get_theme_file_uri($file);
        $ver = md5(filemtime(get_theme_file_path($file)));
        wp_enqueue_style($handle, $src, $deps, $ver, $media);

    function script($handle, $file, $deps=array(), $in_footer='true') {
        $src = get_theme_file_uri($file);
        $ver = md5(filemtime(get_theme_file_path($file)));
        wp_enqueue_script($handle, $src, $deps, $ver, $in_footer);

    style('_normalize', '/assets/css/normalize.css');
    style('_base', '/assets/css/base.css');
    style('_fonts', '//fonts.googleapis.com/css?family=Open+Sans:400,600');

    script('_lightbox2', '//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.9.0/js/lightbox.min.js', array('jquery'));
    script('_base', '/assets/js/base.js', array('jquery'));

    if (is_singular() && comments_open() && get_option('thread_comments')) {
add_action('wp_enqueue_scripts', '_enqueue_scripts');

Solutions Collecting From Web of "Exclude external uri for css and js version"

Declaring functions inside functions can lead to problems as explained here: https://stackoverflow.com/a/1631579/1228379

To solve the issue of remote files, you can use an additional parameter to indicate it is remote (or local) and call get_theme_file_uri/path accordingly. Alternatively, you can just call wp_enqueue_script/style directly and use standard version numbers, or simply save the files locally.

In addition, if you find it simpler, you can also combine the two functions as one and pass some of the parameters as an associative array ($args need only contain the values you want to change):

function myenqueuer($handle, $src, $args) {
    $defaults = array('type'      => 'script',
                      'local'     => true,
                      'deps'      => array(),
                      'media'     => 'all',
                      'in_footer' => true,
                      'ver'       => false);
    $args = wp_parse_args($args, $defaults);
    if($args['local']) {
        $args['ver'] = md5(filemtime(get_theme_file_path($src)));
        $src = get_theme_file_uri($src);
    if($args['type'] == 'style') {
        wp_enqueue_style($handle, $src, $args['deps'], $args['ver'], $args['media']);
    } else if($args['type'] == 'script') {
        wp_enqueue_script($handle, $src, $args['deps'], $args['ver'], $args['in_footer']);

You would call it like this:

myenqueuer('_normalize', '/assets/css/normalize.css', array('type'=>'style'));
myenqueuer('_base', '/assets/css/base.css', array('type'=>'style'));
myenqueuer('_fonts', '//fonts.googleapis.com/css?family=Open+Sans:400,600', array('type'=>'style', 'local'=>false));

myenqueuer('_lightbox2', '//cdnjs.cloudflare.com/ajax/libs/lightbox2/2.9.0/js/lightbox.min.js', array('deps'=>array('jquery'), 'local'=>false));
myenqueuer('_base', '/assets/js/base.js', array('deps'=>array('jquery')));