javascript - Get style and script file versions and use them while writing a cache manifest file

admin2025-06-03  4

Earlier I've learned how to remove file versions from wordpress style and script file source links inside the wp_head function. From here: How to remove file versions from the file source links in wp_head?

I had a good reason needing that action. Why? While writing a cache manifest file, inside that file I managed to write also absolute source links, but without ?ver= parameter. wp_head has generated the used theme with ?ver= parameter the syle and script file inclusions. So there was smooth differences between the cache data and theme data. I don't know if that was the reason not getting the styles while being offline my test site.

Question:

Now what I'd like to know, is the way how can I add ?ver= parameter to the cache manifest file entries. I'm writing this way the cache file's lines:

$hashes = "";
$network = array("\n\nNETWORK:");
$cache = array("\n\nCACHE:");
$path = get_stylesheet_directory()."/";
$dir = new RecursiveDirectoryIterator( $path );

foreach(new RecursiveIteratorIterator($dir) as $file) {
    if ($file->IsFile() && $file != "./manifest.php" && substr($file->getFilename(), 0, 1) != ".") {
        if(preg_match('/.php$/', $file)) {
            if (isAllowedExtension($file))
                array_push($network,"\n" . str_replace($path, './', $file));
        } else {
            if (isAllowedExtension($file))
                array_push($cache,"\n" . str_replace($path, './', $file));
        }

        $hashes .= md5_file($file);
    }
}

Hence this aren't absolute url's, I'm just curious how could be possible adding the ?ver= parameter to this file url's? :)

===

Update:

if( !strpos( $file, '?ver=' ) ) {
    $file = add_query_arg( 'ver', $file );
    return $file;
}

This is only a try... Not complete code. First I think we should check if the file has any version, then add the version parameter after the source url.

Earlier I've learned how to remove file versions from wordpress style and script file source links inside the wp_head function. From here: How to remove file versions from the file source links in wp_head?

I had a good reason needing that action. Why? While writing a cache manifest file, inside that file I managed to write also absolute source links, but without ?ver= parameter. wp_head has generated the used theme with ?ver= parameter the syle and script file inclusions. So there was smooth differences between the cache data and theme data. I don't know if that was the reason not getting the styles while being offline my test site.

Question:

Now what I'd like to know, is the way how can I add ?ver= parameter to the cache manifest file entries. I'm writing this way the cache file's lines:

$hashes = "";
$network = array("\n\nNETWORK:");
$cache = array("\n\nCACHE:");
$path = get_stylesheet_directory()."/";
$dir = new RecursiveDirectoryIterator( $path );

foreach(new RecursiveIteratorIterator($dir) as $file) {
    if ($file->IsFile() && $file != "./manifest.php" && substr($file->getFilename(), 0, 1) != ".") {
        if(preg_match('/.php$/', $file)) {
            if (isAllowedExtension($file))
                array_push($network,"\n" . str_replace($path, './', $file));
        } else {
            if (isAllowedExtension($file))
                array_push($cache,"\n" . str_replace($path, './', $file));
        }

        $hashes .= md5_file($file);
    }
}

Hence this aren't absolute url's, I'm just curious how could be possible adding the ?ver= parameter to this file url's? :)

===

Update:

if( !strpos( $file, '?ver=' ) ) {
    $file = add_query_arg( 'ver', $file );
    return $file;
}

This is only a try... Not complete code. First I think we should check if the file has any version, then add the version parameter after the source url.

Share Improve this question edited Feb 9, 2019 at 0:00 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Apr 18, 2013 at 15:51 user17408user17408
Add a comment  | 

1 Answer 1

Reset to default 0

You could use add_query_arg():

$url = add_query_arg( 'ver', $GLOBALS['wp_version'], $url );

But I see one problem: How can you be sure the resource is actually used? And how do you know the author has not passed a custom version?

So you can do that only in a very controlled environment: when you know everything about the code.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748927970a314893.html

最新回复(0)