0

My version variable doesn't seem to be being applied within my enqueue functions (snippet below).

//version number
$version = '1.0.0';

function frontend_styles($version) {
    wp_enqueue_style(
        'frontend_styles', //reference
        get_stylesheet_directory_uri() . '/assets/css/app.min.css', //source
        array(), //dependenices
        $version, //version number
        'all' //media type ('all', 'screen', 'handheld', 'print')
    );
}

Is there a way of setting a global version number that could be applied to all styles?

EDIT: Below is the actual source code.

//version number
$version = '1.0.0';

//remove jquery scripts
function remove_jquery() {
    wp_deregister_script('jquery');
    wp_deregister_script('jquery-core');
    wp_deregister_script('jquery-migrate');
}

//remove embed scripts
function remove_embed() {
    wp_deregister_script('wp-embed');
}

//site styles
function frontend_styles() {
    global $version;
    wp_enqueue_style(
        'frontend_styles', //reference
        get_stylesheet_directory_uri() . '/assets/css/app.min.css', //source
        array(), //dependenices
        $version, //version number
        'all' //media type ('all', 'screen', 'handheld', 'print')
    );
}

//admin_styles
function backend_styles() {
    global $version;
    wp_enqueue_style(
        'backend_styles', //reference
        get_stylesheet_directory_uri() . '/assets/css/admin.min.css', //source
        array(), //dependenices
        $version, //version number
        'all' //media type ('all', 'screen', 'handheld', 'print')
    );
}

//site scripts
function frontend_scripts() {
    global $version;
    wp_enqueue_script(
        'frontend_scripts', //reference
        get_stylesheet_directory_uri() . '/assets/js/app.min.js', //source
        array(), //dependencies
        $version, //version number
        true //load in footer
    );

    //pass sylsheet uri to javascript variable in app.js
    $translation_array = array(
        'get_stylesheet_directory_uri'  => get_stylesheet_directory_uri() . '/',
        'home_url'                      => home_url('/')
    );

    wp_localize_script('frontend_scripts', 'php', $translation_array);
}

//admin scripts
function backend_scripts() {
    global $version;
    wp_enqueue_script(
        'backend_scripts', //reference
        get_stylesheet_directory_uri() . '/assets/js/admin.min.js', //source
        array(), //dependencies
        $version, //version number
        true //load in footer
    );
}
  • How do you know it is not used? Are you sure it is not another plugin removing the version numbers? – kero Jul 13 '18 at 12:27
  • It doesn't appear at all? So, not appended to the url of your CSS? or does another version appear? – Ovidiu Jul 13 '18 at 12:28
  • It seems that wordpress is reverting to it's default version number. The same way it would if $version was defined as null – Carl Johnston Jul 13 '18 at 12:54
1

Try this

// global theme version
$version = '1.0.0';

function frontend_styles() {

   global $version;
    wp_enqueue_style(
        'frontend_styles', //reference
        get_stylesheet_directory_uri() . '/assets/css/app.min.css', //source
        array(), //dependenices
        $version, //version number
        'all' //media type ('all', 'screen', 'handheld', 'print')
    );
}
  • Hello @florian. In my snippet, WP appends the stylesheet url with "?ver=4.9.7" (the version of WP I've installed). By amending my code with what you suggested it seems to have removed all versioning altogether. Any ideas what might be going on? – Carl Johnston Jul 13 '18 at 12:59
  • Can you check if the version variabele is set within the function and after calling the global $version. – Florian Jul 13 '18 at 13:02
0

Your variable is not defined within the scope of the frontend_styles() function.

If you define $version outside a function, in the global scope, you need to use the global keyword inside the function to access it:

//version number
$version = '1.0.0';

function frontend_styles() {
    global $version;

    wp_enqueue_style(
        'frontend_styles', //reference
        get_stylesheet_directory_uri() . '/assets/css/app.min.css', //source
        array(), //dependenices
        $version, //version number
        'all' //media type ('all', 'screen', 'handheld', 'print')
    );
}
  • @Florian Sorry, rejected your edit initially because I got the before and after mixed up. Have since made the same fix. – Jacob Peattie Jul 13 '18 at 12:36
  • thanks for the response. I've just replied to Florian, but I'll paste the comment below for easy viewing: In my snippet, WP appends the stylesheet url with "?ver=4.9.7" (the version of WP I've installed). By amending my code with what you suggested it seems to have removed all versioning altogether. Any ideas what might be going on? – Carl Johnston Jul 13 '18 at 13:00
  • Is $version defined the same way and in the same place in your actual code? – Jacob Peattie Jul 13 '18 at 13:02
  • ``` Source code: //version number $version = '1.0.0'; //remove embed scripts function remove_embed() { wp_deregister_script('wp-embed'); } //site styles function frontend_styles($version) { wp_enqueue_style( 'frontend_styles', //reference get_stylesheet_directory_uri() . '/assets/css/app.min.css', //source array(), //dependenices $version, //version number 'all' //media type ('all', 'screen', 'handheld', 'print') ); } ``` – Carl Johnston Jul 13 '18 at 13:03
  • Could you add that to the question so that it can be formatted to be more readable? – Jacob Peattie Jul 13 '18 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.