Discover in this tutorial how to display all the information you want from any theme from the WordPress repository using the API.
In this tutorial you are going to use the WordPress.org API to get all the associated data you are interested in of a WordPress theme (name, ratings, active installs, version, description…) and display it as you want.
This would be the final result:
Shall we start?
Steps to display theme information from the WordPress repository
1. Create a shortcode with the theme attribute from the repository
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 the above code you create a shortcode with the slug
attribute that allows you to indicate the WordPress repository theme slug. Once the plugin is defined, you query the WordPress.org API to extract the data you are interested in.
In this example you “paint” the following data:
- Plugin name (with link).
- Rating (indicated with number of stars).
- Number of ratings.
- Active installations.
- Downloads.
- Theme version.
- Minimum WordPress version required.
- Description.
- Download button.
Obviously, you can modify what you see fit to display the data you consider convenient.
Unlike the API for plugins, the number of active installations is not visible by default, so in the code above you add some extra parameters (&request[fields][active_installs]=true
) so you can get it.
In the example we are only getting the data of 11 endpoints but with the API, when you make the request for information on a topic, you can access all of them:
name
slug
version
preview_url
author
user_nicename
profile
avatar
display_name
author
author_url
screeshot_url
rating
num_ratings
reviews_url
requires_plugins
support_threads
support_threads_resolved
downloaded
active_installs
last_updated
last_updated_time
creation_time
homepage
sections
description
download_link
screenshots
tags
requires
requires_php
is_commercial
external_support_url
is_community
external_repository_url
If you have doubts about the slug of a theme you can visit its WordPress repository page. The URL will be: https://wordpress.org/themes/theme-slug.
2. Customize how the theme information is displayed
In the code above you not only have all the functional part but you also add CSS classes to the different data you display. This allows you to modify the appearance of each of them to suit your needs.
3. Insert the shortcode where you want
Finally, just type[theme_info slug="osom-business"]
where you want it to be displayed and change the theme slug to the one you want to display.
This would be an example of the information that would display the shortcode if you put slug="osom-business"
:
Conclusions
Now you know how to display the information you are interested in from any theme in the WordPress repository using its API.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂