Skip to content

Instantly share code, notes, and snippets.

@marsjaninzmarsa
Created January 2, 2020 05:53
Show Gist options
  • Select an option

  • Save marsjaninzmarsa/038ce27f1e76b90edd5773532e48520b to your computer and use it in GitHub Desktop.

Select an option

Save marsjaninzmarsa/038ce27f1e76b90edd5773532e48520b to your computer and use it in GitHub Desktop.

Revisions

  1. marsjaninzmarsa created this gist Jan 2, 2020.
    27 changes: 27 additions & 0 deletions version-string.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    <?php

    /**
    * Replaces query version in registered scripts or styles with file modified time
    *
    * @link https://wordpress.stackexchange.com/a/346024/51130 based on
    *
    * @param $src
    *
    * @return string
    */
    function add_modified_time( $src ) {
    $clean_src = remove_query_arg( 'ver', $src );
    $path = wp_parse_url( $src, PHP_URL_PATH );
    $filename = untrailingslashit( ABSPATH ) . '/..' . $path;
    if ( $modified_time = @filemtime( $filename ) ) {
    $src = add_query_arg( 'ver', $modified_time, $clean_src );
    } else {
    // $src = add_query_arg( 'ver', time(), $clean_src );
    }

    return $src;

    }

    add_filter( 'style_loader_src', 'add_modified_time', 99999999, 1 );
    add_filter( 'script_loader_src', 'add_modified_time', 99999999, 1 );