Find out the version number of a WordPress theme

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
Search for a command to run...

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
No comments yet. Be the first to comment.
Recently, a friend asked me to send a script to remove pre tags from a string, including content. He uses a script to calculate the reading time for the content, but doesn't want the pre-tags to be included in the scoring. So if you ever have a simil...

When there are problems with one's WordPress website, it happens that one has to create a user without having access to the database or the WordPress admin. Then it is useful to be able to create or log in a user using FTP. So here you will learn how...

Occasionally it happens that overlaps occur in a large table and the message Duplicate entry '0' for key 'PRIMARY' for query then gets the upper hand in the error logs. This error can be fixed quite easily with a short line of SQL. Fix duplicate entr...

WordPress is packed with useful features. It is easy to lose the overview. Here you will find a selection of functions that deal with arrays and objects. A small collection of useful functions that you can always use, if you know that they exist. wp_...

As a rule, searches are created with the MySQL LIKE. This usually works very well, as long as the database table is not too large. However, if you work with a large database (millions+ entries), then you quickly notice how long a search in it actuall...

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.
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.