Adel Abdellatif
Adel Abdellatif

Reputation: 58

can't change version using wp_enqueue_style or anything

I've enqueued a style sheet using

wp_enqueue_style( 'home-custom-style', get_stylesheet_directory_uri() . 
'/css/style.css');

in my child theme then when I tried to modify it, I've realised that wordpress gave it an automatic version, which now I can't change.

I have tried modifying the enqueue code adding a version,

wp_enqueue_style( 'home-custom-style', get_stylesheet_directory_uri() . 
'/css/style.css' , array(), '30140222');

also tried dequeuing it and enqueuing it again, and deleting the file and readding it in the folder but nothing is working. except null.

For now I made a hard refresh (bypassing my cache) pressing Ctrl + F5 in the browser, cause I'm on the local host, but still need an answer to use it on the server.

Upvotes: 2

Views: 3389

Answers (3)

Arik
Arik

Reputation: 6501

You should try to search your themes & plugins folders for functions that modify your enqueued styles and scripts.

I was looking for lines containing style_loader_src or ver= and discovered a plugin called "Child Theme Configuratior" that was the culprit.

Turned that plugin off and everything started to work as expected.

Upvotes: 1

Nikola Ivanov Nikolov
Nikola Ivanov Nikolov

Reputation: 5062

heads-up the following applies to scripts too, just replace style with script and you're good to go

The problem is that you're trying to modify an already registered style. See, when you call wp_enqueue_style(), the style is registered and enqueued at the same time(vs calling wp_register_style() and then wp_enqueue_style() which I recommend).

However, if a style has already been registered, when you try to enqueue it the extra parameters are not used, they're just ignored.

What you need to do is the following( make sure to fill in the correct parameters in the wp_register_style() below):

wp_deregister_style( 'my-style' );
wp_register_style( 'my-style', ... );
wp_enqueue_style( 'my-style' );

This first deregisters the still you want to modify and then registers it again with the modified parameters. This will work with styles that are used as a dependency or have dependencies of their own, because dependencies are resolved at the time the style is being loaded on the page and not when it's registered/enqueued.

For what you're trying to achieve though, I would personally just register whatever scripts and styles I want to override in the child theme before the parent theme does it. That way you won't have to deregisters and register them. Here's an example(part of it is the sample parent theme's code):

function parent_theme_enqueue_scripts(){
    wp_enqueue_style( 'example-style', get_bloginfo( 'template_directory' ) . '/css/example.css', array(), 'v1.0.0' );
}
add_action( 'wp_enqueue_scripts', 'parent_theme_enqueue_scripts', 10 );

function child_theme_enqueue_scripts(){
    wp_register_style( 'example-style', get_bloginfo( 'stylesheet_directory' ) . '/css/example.css', array(),'v1.2.0' );
}
add_action( 'wp_enqueue_scripts', 'child_theme_enqueue_scripts', 5 );

The important thing to notice here is the priority on child_theme_enqueue_scripts - it's lower than that of parent_theme_enqueue_scripts. Also note that we're not directly enqueue-ing the style, but instead we're just registering it. That's in order to not mess up the loading order of styles(in case the parent theme enqueues multiple styles but doesn't use dependencies to do that).

Upvotes: 2

Minal Chauhan
Minal Chauhan

Reputation: 6158

Just add this function in function.php

function wpa() {
    wp_enqueue_style( 'home-custom-style', get_stylesheet_directory_uri() . '/css/style.css' , array(), '30140222' );
}
add_action('wp_enqueue_scripts', 'wpa');

Upvotes: 0

Related Questions