Recently I needed a way to display the version number of the current theme for
admin_notices in WordPress. Since WordPress is a stuffed monster, it has something for this too. With
wp_get_theme() you get the information of your theme. You can also specify other paths or folder names to get information from other themes.
Output the version number of your WordPress theme
First you need to know how
wp_get_theme() is built. This function returns a so called
WP_Theme Object. It contains all information about the desired WordPress theme, if it exists.
/** * @param string $stylesheet Folder name of your WordPress theme. Optional. * @param string $theme_root Absolute path of your WordPress theme. Optional. */ $theme = wp_get_theme( string $stylesheet = '', string $theme_root = '' );
If you use
wp_get_theme() without parameters, your current WordPress theme is always used. In our case we want that too. So let's get our version number and also the name of our theme:
$theme = wp_get_theme(); /* Checks if the theme exists */ if ( false !== $theme->exists() ) : /* Version number */ echo esc_html( $theme->get( 'Version' ) ); /* Name */ echo esc_html( $theme->get( 'Name' ) ); endif;
Now you know how to find out the version and name of your theme. For example, you can now always save the current version in an option when you update, and each time you update the theme, you can check if the version differs from the saved version and perform actions. For example, you can now output
admin_notices, which inform your user about certain things after the user has updated the WordPress theme.
Did you find this article valuable?
Support Kevin Pliester by becoming a sponsor. Any amount is appreciated!