the wp_get_theme()->get( 'Version' ) is not working properly

i am devolping a theme localy using xampp and i have enqued my stylesheet as following:

function hireme_register_styles() {
	$theme_version = wp_get_theme()->get( 'Version' );

	wp_enqueue_style( 'hireme-style', get_stylesheet_uri(), array(), $theme_version );
	wp_style_add_data( 'hireme-style', 'rtl', 'replace' );
}
add_action( 'wp_enqueue_scripts', 'hireme_register_styles' );

based on 2020 theme. but when i close the project then open it again. i should delete the $theme_version variable that the theme can detect changes.

where is the problem please ?

Hi @ferhaneriyadh

you can use the following code to get theme version:

$theme = wp_get_theme( );
$theme_name = $theme->parent_theme;
$theme_data = get_theme_data( get_theme_root() . ‘/’ . $theme_name . ‘/style.css’ );
$theme_version = $theme_data[‘Version’];

Thanks

1 Like

Do it like this:

$version = wp_get_theme( 'your-parent-theme-slug' );
$version = $version->Version;

If you do not specify the parent theme slug, then the child theme version will be used. But you do not want a child theme version for parent theme stylesheet.

Then in the child theme functions.php, do it like this:

$parent_version = wp_get_theme( 'your-parent-theme-slug' );
$parent_version = $parent_version->Version;

$child_version = wp_get_theme();
$child_version = $child_version->Version;

And load both parent and child theme stylesheets separately with the correct version.

1 Like

WordPress adds the current version to the CSS files already. This could be the problem as you may need to overwrite few details but why do you think this is necessary ?

1 Like

By default, WordPress adds the current version of the WordPress, not the theme. It is very important to use the parent theme’s version instead because of the browser (or server) caching. Don’t do that and you are guaranteed to get many support requests from customers who updated the theme but don’t see any changes in the CSS.

1 Like

worked as charm. but get_theme_data is deprecated i changed it with wp_get_theme

thenks a lot

1 Like