Mesa de escritório com um monitor grande com código sendo exibido em modo escuro. Na mesa há um bloco de notas com uma caneta, uma xícara branca de café, um laptop e uma luminária.

List the name of the active theme in WordPress using WP-CLI

In a recent PainelWP issue, Daniel Kossmann explained how to find the active theme on WordPress using WP-CLI. In this post, I will share my favorite command to do the same.

In the 290th issue of PainelWP, Daniel Kossmann shared an article teaching how to list the active theme on a WordPress site using WP-CLI with the option command:

$ wp option get current_theme

Although this is a quick way to obtain the active theme name on the site, it can cause some “problems” if you need to use the name returned by the command in another WP-CLI command.

$ wp option get current_theme
Blocksy

$ wp theme get Blocksy
Error: The 'Blocksy' theme could not be found. Did you mean 'blocksy'?

In the example above, the Blocksy theme is active; however, it cannot be found because Blocksy is not the slug of the theme in wp-content/themes. This happens because the current_theme record in the database saves the Theme Name as specified in the style.css file in the theme folder.

When using the command to list the themes and filter by the active theme, we receive information that the theme’s name field is blocksy with a lowercase b and not Blocksy with an uppercase B:

$ wp theme list --status=active
+---------+--------+--------+---------+----------------+-------------+
| name    | status | update | version | update_version | auto_update |
+---------+--------+--------+---------+----------------+-------------+
| blocksy | active | none   | 2.0.84  |                | off         |
+---------+--------+--------+---------+----------------+-------------+

$ wp theme get blocksy
+----------------+-----------------------------------------------------------------------+
| Field          | Value                                                                 |
+----------------+-----------------------------------------------------------------------+
| name           | Blocksy                                                               |
| title          | Blocksy                                                               |
| version        | 2.0.84                                                                |
| status         | active                                                                |
| parent_theme   |                                                                       |
| template_dir   | /srv/htdocs/wp-content/themes/blocksy                                 |
| stylesheet_dir | /srv/htdocs/wp-content/themes/blocksy                                 |
| template       | blocksy                                                               |
| stylesheet     | blocksy                                                               |
| screenshot     | screenshot.jpg                                                        |
| description    | Blocksy is a blazing fast and lightweight WordPress theme built with  |
|                | the latest web technologies. It was built with the Gutenberg editor i |
|                | n mind and has a lot of options that makes it extendable and customiz |
|                | able. You can easily create any type of website, such as business age |
|                | ncy, shop, corporate, education, restaurant, blog, portfolio, landing |
|                |  page and so on. It works like a charm with popular WordPress page bu |
|                | ilders, including Elementor, Beaver Builder, Visual Composer and Briz |
|                | y. Since it is responsive and adaptive, translation ready, SEO optimi |
|                | zed and has WooCommerce built-in, you will experience an easy build a |
|                | nd even an increase in conversions.                                   |
| author         | <a href="https://creativethemes.com">CreativeThemes</a>               |
| tags           | ["blog","e-commerce","wide-blocks","block-styles","grid-layout","one- |
|                | column","two-columns","three-columns","four-columns","right-sidebar", |
|                | "left-sidebar","translation-ready","custom-colors","custom-logo","cus |
|                | tom-menu","featured-images","footer-widgets","full-width-template","t |
|                | heme-options","threaded-comments","buddypress","rtl-language-support" |
|                | ,"news"]                                                              |
| theme_root     | /srv/htdocs/wp-content/themes                                         |
| theme_root_uri | http://rafaelfunchal.com.br/wp-content/themes                         |
+----------------+-----------------------------------------------------------------------+

Although the problem seems minor with the Blocksy theme, things start to get interesting when we work with themes that have more than one word, hyphens, and numbers like these:

$ wp option get current_theme
Radcliffe 2

$ wp option get current_theme
Twenty Twenty-Five

The slugs/names of these themes in wp-content/themes and accepted by WP-CLI are radcliffe-2 and twentytwentyfive, respectively.


My command suggestion

If you need to know the active theme on a WordPress site using WP-CLI and receive the information ready for other wp theme commands such as get, update, install, path, and delete, I recommend using this command:

$ wp theme list --status=active --field=name

It will return the theme’s slug in the way that WP-CLI needs. Some examples:

$ wp theme list --status=active --field=name
blocksy

$ wp theme list --status=active --field=name
radcliffe-2

$ wp theme list --status=active --field=name
twentytwentyfive

Do you know other ways to list the active theme in WP-CLI? What is your preferred method?

I hope this article has been helpful <3


PS: Today (January 20th), I noticed Felipe Elia thought of the same approach and even commented on the original post.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Rafael Funchal

Subscribe now to keep reading and get access to the full archive.

Continue reading