Discover in this tutorial how to get and display all themes from a remote WordPress installation using the REST API.
In some cases you may be interested in displaying (or getting) all the themes of a remote WordPress installation, including the version, author or other data.
So in this tutorial you are going to learn how you can get a listing of all the themes of a WordPress installation using the REST API. This would be the result:
Let’s see it:
Steps to display all themes in a WordPress installation using the REST API
1. Create a password for the REST API
The first thing you need to do is create a new application password.
To do this go (in the WordPress installation where you want to authenticate) to Users > Profile and scroll down to the “Application Passwords” section.
Then fill in the new password name field (it can be anything) and click on “Add a new application password”.
Once done, the password name and password will be displayed
Make sure to copy it and keep it safe because it will not be displayed again.
2. Get the endpoint data and display it with a shortcode
Once you have the credentials to be able to authenticate with the REST API you can make the call to the endpoint to get the information about the topics.
In addition, you are going to encapsulate this data inside a shortcode to be able to display the list wherever you want.
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
With this code you create a shortcode that allows you to display a list with all the themes of a remote WordPress installation using the [theme_list] shortcode.
In the first part of the snippet you define the variables:
username
= WordPress username$password
= Application password obtained in the previous step$website_url
= The URL of the website$request_url
= The URL where the request is made. In this case it is the concatenation:$website_url . '/wp-json/wp/v2/themes/'
which accesses the endpoint for REST API themes.
Remember to change the value of the following variables to those of your case.
Then you use the wp_remote_request
function to get the endpoint for themes (with the GET
method).
The authentication part is done in the headers using the base64_encode
function to encrypt the username and password.
Once you get the information sent by the API you use the wp_remote_retrieve_body
functions to extract the data from the body and json_decode
to decode it.
And when you have the array with the data, you use a foreach loop to extract the information you are interested in.
In this case, 3 details about the theme information:
- Theme name
[name->rendered]
> with link to it[theme_uri->rendered]
- Theme version
[version]
- Author’s name
[author->rendered]
> with link to it[author_uri->rendered]
But you can remove or add more to customize it to your liking. These are all the details you can display:
[stylesheet]
[template]
[name]
[theme_uri]
[author]
[author_uri]
[description]
[version]
[tags]
[requires_wp]
[requires_php]
[textdomain]
[theme_supports]
[screenshot]
[status]
You have more information about it on the official WordPress developer page.
For example, for Twenty Twenty-Two theme it returns something like this:
Array ( [0] => stdClass Object ( [href] => https://tuweb.com/index.php/wp-json/wp/v2/themes ) ) ) ) [3] => stdClass Object (
[stylesheet] => twentytwentytwo
[template] => twentytwentytwo
[requires_php] => 5.6
[requires_wp] => 5.9
[textdomain] => twentytwentytwo
[version] => 1.0
[screenshot] => https://yoursite.com/wp-content/themes/twentytwentytwo/screenshot.png
[author] => stdClass Object ( [raw] => the WordPress team [rendered] => the WordPress team )
[author_uri] => stdClass Object ( [raw] => https://wordpress.org/ [rendered] => https://wordpress.org/ )
[description] => stdClass Object ( [raw] => Built on a solidly designed foundation, Twenty Twenty-Two embraces the idea that everyone deserves a truly unique website. The theme’s subtle styles are inspired by the diversity and versatility of birds: its typography is lightweight yet strong, its color palette is drawn from nature, and its layout elements sit gently on the page. The true richness of Twenty Twenty-Two lies in its opportunity for customization. The theme is built to take advantage of the Full Site Editing features introduced in WordPress 5.9, which means that colors, typography, and the layout of every single page on your site can be customized to suit your vision. It also includes dozens of block patterns, opening the door to a wide range of professionally designed layouts in just a few clicks. Whether you’re building a single-page website, a blog, a business website, or a portfolio, Twenty Twenty-Two will help you create a site that is uniquely yours. [rendered] => Built on a solidly designed foundation, Twenty Twenty-Two embraces the idea that everyone deserves a truly unique website. The theme’s subtle styles are inspired by the diversity and versatility of birds: its typography is lightweight yet strong, its color palette is drawn from nature, and its layout elements sit gently on the page. The true richness of Twenty Twenty-Two lies in its opportunity for customization. The theme is built to take advantage of the Full Site Editing features introduced in WordPress 5.9, which means that colors, typography, and the layout of every single page on your site can be customized to suit your vision. It also includes dozens of block patterns, opening the door to a wide range of professionally designed layouts in just a few clicks. Whether you’re building a single-page website, a blog, a business website, or a portfolio, Twenty Twenty-Two will help you create a site that is uniquely yours. )
[name] => stdClass Object ( [raw] => Twenty Twenty-Two [rendered] => Twenty Twenty-Two )
[tags] => stdClass Object ( [raw] => Array ( [0] => one-column [1] => custom-colors [2] => custom-menu [3] => custom-logo [4] => editor-style [5] => featured-images [6] => full-site-editing [7] => block-patterns [8] => rtl-language-support [9] => sticky-post [10] => threaded-comments ) [rendered] => one-column, custom-colors, custom-menu, custom-logo, editor-style, featured-images, full-site-editing, block-patterns, rtl-language-support, sticky-post, threaded-comments )
[theme_uri] => stdClass Object ( [raw] => https://github.com/wordpress/twentytwentytwo/ [rendered] => https://github.com/wordpress/twentytwentytwo/ )
[status] => inactive
[_links] => stdClass Object ( [self] => Array ( [0] => stdClass Object ( [href] => https://tuweb.com/index.php/wp-json/wp/v2/themes/twentytwentytwo ) )
[collection] => Array ( [0] => stdClass Object ( [href] => https://tuweb.com/index.php/wp-json/wp/v2/themes ) ) ) ) )
So you can customize the details you prefer to be displayed.
If you have any questions or suggestions you can leave them in the comments.
There is only one step left to view the list.
2. Insert the shortcode where you want
The advantage of shortcodes is that you can insert them anywhere on the website.
Just type [theme_list]
where you want it to be displayed.
Important:
- If you are using the classic editor, use the HTML tab to insert the shortcode.
- In case you are already using the new block editor (Gutenberg) you can add it to the normal paragraph block, the HTML block or the shortcode block. All of them will render the content correctly.
This would be an example of the listing that the shortcode would display:
Conclusions
Now you know how to use the REST API to get and display all the themes from a remote WordPress installation.
Any questions? Let me know in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉