innocode-digital/wp-assets-version

Helps with versioning of CSS and JS files.

Installs: 5 715

Dependents: 2

Suggesters: 0

Security: 0

Stars: 1

Watchers: 11

Forks: 1

Type:wordpress-muplugin

1.1.0 2022-10-05 13:19 UTC

This package is auto-updated.

Last update: 2024-04-05 17:02:08 UTC


README

Description

Helps with versioning of CSS and JS files. Plugin adds flush buttons to admin area for version upgrading. Requires Flush Cache Buttons plugin.

Install

  • Preferable way is to use Composer:

    composer require innocode-digital/wp-assets-version
    

    By default, it will be installed as Must Use Plugin. It's possible to control with extra.installer-paths in composer.json.

  • Alternate way is to clone this repo to wp-content/mu-plugins/ or wp-content/plugins/:

    cd wp-content/plugins/
    git clone git@github.com:innocode-digital/wp-assets-version.git
    cd wp-assets-version/
    composer install
    

If plugin was installed as regular plugin then activate Assets Version from Plugins page or WP-CLI: wp plugin activate wp-assets-version.

Usage

There are known functions wp_enqueue_script and wp_enqueue_style in WordPress core to register and enqueue scripts and styles. In both of them 4th parameter is for version number and in ideal case it should be version of build or theme or null with some version hash in filename ... but sometimes it's hard to implement or there are could be particular issues in caching, and it's where this plugin becomes as a good hotfix. There are few ways to add version with this plugin:

  • Retrieve version through function innocode_assets_version() and set as 4th parameter in enqueue functions, e.g.:

    $ver = function_exists( 'innocode_assets_version' ) ? innocode_assets_version() : false;
    
    wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
    
  • Force using of version where null set as 4th parameter:

    add_filter( 'innocode_assets_version_allow_default', '__return_true' );
    
  • Force using of version but with own logic per dependency (requires previous hook to be set), e.g.:

    /**
     * @param bool $allow
     * @param string $type - One of [ 'script', 'style' ].
     * @param _WP_Dependency $dependency
     */
    add_filter( 'innocode_assets_version_allow_dependency', function ( bool $allow, string $type, _WP_Dependency $dependency ) {
        // @TODO: implement logic
    
        return $allow;
    }, 20, 3 );