Checking the Current WordPress Version via Command Line with(out) WP-CLI

There are so many ways to check the current WordPress version mainly using the graphical user interface (GUI) dashboard. Usually I am working on the command line so I prefer to be able to check without having to log in to the site. Below I show 3 different ways that you can check the current WordPress version from the Linux command line you can access via SSH.

Grep

Using grep it could not be easier since we are checking the version.php file directly

grep wp_version wp-includes/version.php

Here is the output!

* @global string $wp_version
$wp_version = '4.9.9';

We can combine this with awk to extract just the version

grep wp_version wp-includes/version.php | awk -F "'" '{print $2}'

Now you will get cleaner output showing just the version number

4.9.9

This is by far the easiest method but in case you don’t have grep here are some alternatives.

WP-CLI

The simplest way to check the WordPress core version with WP-CLI is to use the core version command

wp core version --allow-root

You will see output like this

5.0.2

We can also get more advanced using the wp option pluck command for the _site_transient_update_core value

wp option pluck _site_transient_update_core current --allow-root

Output showing the WordPress version using the database to find out via WP-CLI!

4.9.9

This is how the command was constructed, first I got the entire option and put it in yaml format

wp option get _site_transient_update_core --format=yaml --allow-root

Output

updates:
  -
    response: latest
    download: https://downloads.wordpress.org/release/wordpress-5.0.2.zip
    locale: en_US
    packages:
      full: https://downloads.wordpress.org/release/wordpress-5.0.2.zip
      no_content: https://downloads.wordpress.org/release/wordpress-5.0.2-no-content.zip
      new_bundled: https://downloads.wordpress.org/release/wordpress-5.0.2-new-bundled.zip
      partial: false
      rollback: false
    current: 5.0.2
    version: 5.0.2
    php_version: 5.2.4
    mysql_version: "5.0"
    new_bundled: "5.0"
    partial_version: ""
last_checked: 1545492666
version_checked: 5.0.2
translations: [ ]

Based on this structure I could see that the version_checked value would be easiest to extract since it was at the top of the hierarchy (nested arrays can be time consuming to pluck from).

Using MySQL Command Line

For this section you will need the following variables from your wp-config.php

  • DB_NAME
  • DB_USER
  • DB_PASSWORD

You can then insert them into this MySQL command executed from the shell (bash in this example but any should work). Be sure to change your database prefix in the wp_options text below to reflect your custom database prefix if you use one.

mysql -u DB_USER -p"DB_PASSWORD" -e "USE DB_NAME; SELECT option_value FROM wp_options WHERE option_name='_site_transient_update_core';" --skip-column-names |   awk -F '"' '{ print $42 }'

Here is the output showing the version value from the serialized array inside of the _site_transient_update_core from the option_name column using MySQL query and awk.

5.0.2

Please remember to clear your bash history to erase the MySQL password

cat /dev/null > ~/.bash_history

In case you are curious, this is how the MySQL command above was constructed. First we need to examine the whole structure of the serialized array

+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| option_value                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| O:8:"stdClass":4:{s:7:"updates";a:1:{i:0;O:8:"stdClass":10:{s:8:"response";s:6:"latest";s:8:"download";s:59:"https://downloads.wordpress.org/release/wordpress-5.0.2.zip";s:6:"locale";s:5:"en_US";s:8:"packages";O:8:"stdClass":5:{s:4:"full";s:59:"https://downloads.wordpress.org/release/wordpress-5.0.2.zip";s:10:"no_content";s:70:"https://downloads.wordpress.org/release/wordpress-5.0.2-no-content.zip";s:11:"new_bundled";s:71:"https://downloads.wordpress.org/release/wordpress-5.0.2-new-bundled.zip";s:7:"partial";b:0;s:8:"rollback";b:0;}s:7:"current";s:5:"5.0.2";s:7:"version";s:5:"5.0.2";s:11:"php_version";s:5:"5.2.4";s:13:"mysql_version";s:3:"5.0";s:11:"new_bundled";s:3:"5.0";s:15:"partial_version";s:0:"";}}s:12:"last_checked";i:1545492666;s:15:"version_checked";s:5:"5.0.2";s:12:"translations";a:0:{}} |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

We can see there is this section which would get us the current WordPress version

"current";s:5:"5.0.2"

Using awk we can split the serialized array into fields using " as the separator and then print the 42nd field which is 5.0.2 shown above.

awk -F '"' '{ print $42 }'

Sources

Clear bash history completely