how does wordpress get the information about installed themes?

If you log in WordPress backend and click Appearance/Themes menu, you will see all installed themes. If you put a theme directory under wp-content/themes/, you will see the new theme in WordPress dashboard. To delete an installed theme, just remove the theme directory. How does wordpress do that?

We have known how wordpress gets the information of installed plugin, wordpress gets the information of installed themes in a similar way. The first time to scan the theme directory to get the information about installed themes is in wp-admin/admin.php by doing the action “load-themes.php”. In this action, wp_update_themes is called to update installed themes, so it must know what themes are installed in the theme directory. It calls wp_get_themes to realize this. wp_get_themes calls search_theme_directories to scan all sub-directories under the theme directory. If a sub-directory has a file named style.css in it, it is considered a valid theme directory, and a new entry is added to a static array $found_themes with the name of the sub-directory as the key. style.css is not only a css file but also a theme description(info)  file with a lot of theme information in the comments such as theme name, theme uri, description, author, license, etc. The reason for using a static array is that the found themes will be used later so directory scanning can be avoid. Likewise, wp_get_themes stores the information of themes in a static array $_themes for later use. $_themes takes the full path name of the theme’s directory as the key and a WP_Theme object as the value. WP_Theme object has an info field template(i.e., the feature strings in the style.css) by which the values of the info fields are extracted from style.css under the theme’s directory and stored in the WP_Theme object(as the headers member). WP_Theme’s template and stylesheet members are set to the name of the theme’s directory. To be a valid theme, there must be an index.php under the theme’s directory. The information of all installed themes are posted to http://api.wordpress.org/themes/update-check/1.1/ to check their update information.

The second time to get the information of all installed themes is in wp-admin/themes.php in the call to function wp_prepare_themes_for_js(). This time, it need not scan the theme directory again, thanks to the static arrays mentioned above. wp_prepare_themes_for_js() returns a $prepared_themes array with the theme’s slug(i.e., the name of the theme’s directory) as the key and the info array of the theme as the value. We can see screenshots of themes in the wordpress dashboard. The screenshot is taken as the file screenshot.png(gif,jpg,jpeg) in the theme’s directory, which is put into the info array together with a lot of other information. All the information is used to display the themes in the wordpress dashboard for you to switch to a new theme or customize the active theme. So if your wordpress theme screenshot not showing, or you want to change the screenshot image, or adjust the size, just check/modify screenshot.png in the theme directory. Note that the theme name under the screenshot is taken from the info array(got from style.css), not the name of the theme directory. So if you have two copies of the same theme under the theme directory, the theme names in the dashboard keep the same and you can not tell which directory a theme corresponds to.

1 Comment

  1. steven

    Hi,

    I just found your website and it’s very helpful, hope you can keep posting. Thanks!

Leave a Reply