if ( ! isset( $args->per_page ) ) {
if ( ! isset( $args->locale ) ) {
$args->locale = get_user_locale();
if ( ! isset( $args->wp_version ) ) {
$args->wp_version = substr( wp_get_wp_version(), 0, 3 ); // x.y
* Filters arguments used to query for installer pages from the WordPress.org Themes API.
* Important: An object MUST be returned to this filter.
* @param object $args Arguments used to query for installer pages from the WordPress.org Themes API.
* @param string $action Requested action. Likely values are 'theme_information',
* 'feature_list', or 'query_themes'.
$args = apply_filters( 'themes_api_args', $args, $action );
* Filters whether to override the WordPress.org Themes API.
* Returning a non-false value will effectively short-circuit the WordPress.org API request.
* If `$action` is 'query_themes', 'theme_information', or 'feature_list', an object MUST
* be passed. If `$action` is 'hot_tags', an array should be passed.
* @param false|object|array $override Whether to override the WordPress.org Themes API. Default false.
* @param string $action Requested action. Likely values are 'theme_information',
* 'feature_list', or 'query_themes'.
* @param object $args Arguments used to query for installer pages from the Themes API.
$res = apply_filters( 'themes_api', false, $action, $args );
$url = 'http://api.wordpress.org/themes/info/1.2/';
$ssl = wp_http_supports( array( 'ssl' ) );
$url = set_url_scheme( $url, 'https' );
'user-agent' => 'WordPress/' . wp_get_wp_version() . '; ' . home_url( '/' ),
$request = wp_remote_get( $url, $http_args );
if ( $ssl && is_wp_error( $request ) ) {
if ( ! wp_doing_ajax() ) {
/* translators: %s: Support forums URL. */
__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
__( 'https://wordpress.org/support/forums/' )
) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
$request = wp_remote_get( $http_url, $http_args );
if ( is_wp_error( $request ) ) {
/* translators: %s: Support forums URL. */
__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
__( 'https://wordpress.org/support/forums/' )
$request->get_error_message()
$res = json_decode( wp_remote_retrieve_body( $request ), true );
if ( is_array( $res ) ) {
// Object casting is required in order to match the info/1.0 format.
} elseif ( null === $res ) {
/* translators: %s: Support forums URL. */
__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
__( 'https://wordpress.org/support/forums/' )
wp_remote_retrieve_body( $request )
if ( isset( $res->error ) ) {
$res = new WP_Error( 'themes_api_failed', $res->error );
if ( ! is_wp_error( $res ) ) {
// Back-compat for info/1.2 API, upgrade the theme objects in query_themes to objects.
if ( 'query_themes' === $action ) {
foreach ( $res->themes as $i => $theme ) {
$res->themes[ $i ] = (object) $theme;
// Back-compat for info/1.2 API, downgrade the feature_list result back to an array.
if ( 'feature_list' === $action ) {
* Filters the returned WordPress.org Themes API response.
* @param array|stdClass|WP_Error $res WordPress.org Themes API response.
* @param string $action Requested action. Likely values are 'theme_information',
* 'feature_list', or 'query_themes'.
* @param stdClass $args Arguments used to query for installer pages from the WordPress.org Themes API.
return apply_filters( 'themes_api_result', $res, $action, $args );
* Prepares themes for JavaScript.
* @param WP_Theme[] $themes Optional. Array of theme objects to prepare.
* Defaults to all allowed themes.
* @return array An associative array of theme data, sorted by name.
function wp_prepare_themes_for_js( $themes = null ) {
$current_theme = get_stylesheet();
* Filters theme data before it is prepared for JavaScript.
* Passing a non-empty array will result in wp_prepare_themes_for_js() returning
* early with that value instead.
* @param array $prepared_themes An associative array of theme data. Default empty array.
* @param WP_Theme[]|null $themes An array of theme objects to prepare, if any.
* @param string $current_theme The active theme slug.
$prepared_themes = (array) apply_filters( 'pre_prepare_themes_for_js', array(), $themes, $current_theme );
if ( ! empty( $prepared_themes ) ) {
// Make sure the active theme is listed first.
$prepared_themes[ $current_theme ] = array();
if ( null === $themes ) {
$themes = wp_get_themes( array( 'allowed' => true ) );
if ( ! isset( $themes[ $current_theme ] ) ) {
$themes[ $current_theme ] = wp_get_theme();
if ( ! is_multisite() && current_user_can( 'update_themes' ) ) {
$updates_transient = get_site_transient( 'update_themes' );
if ( isset( $updates_transient->response ) ) {
$updates = $updates_transient->response;
if ( isset( $updates_transient->no_update ) ) {
$no_updates = $updates_transient->no_update;
WP_Theme::sort_by_name( $themes );
$auto_updates = (array) get_site_option( 'auto_update_themes', array() );
foreach ( $themes as $theme ) {
$slug = $theme->get_stylesheet();
$encoded_slug = urlencode( $slug );
if ( $theme->parent() ) {
$parent = $theme->parent();
$parents[ $slug ] = $parent->get_stylesheet();
$parent = $parent->display( 'Name' );
$customize_action = null;
$can_edit_theme_options = current_user_can( 'edit_theme_options' );
$can_customize = current_user_can( 'customize' );
$is_block_theme = $theme->is_block_theme();
if ( $is_block_theme && $can_edit_theme_options ) {
$customize_action = admin_url( 'site-editor.php' );
if ( $current_theme !== $slug ) {
$customize_action = add_query_arg( 'wp_theme_preview', $slug, $customize_action );
} elseif ( ! $is_block_theme && $can_customize && $can_edit_theme_options ) {
$customize_action = wp_customize_url( $slug );
if ( null !== $customize_action ) {
$customize_action = add_query_arg(
'return' => urlencode( sanitize_url( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ),
$customize_action = esc_url( $customize_action );
$update_requires_wp = isset( $updates[ $slug ]['requires'] ) ? $updates[ $slug ]['requires'] : null;
$update_requires_php = isset( $updates[ $slug ]['requires_php'] ) ? $updates[ $slug ]['requires_php'] : null;
$auto_update = in_array( $slug, $auto_updates, true );
$auto_update_action = $auto_update ? 'disable-auto-update' : 'enable-auto-update';
if ( isset( $updates[ $slug ] ) ) {
$auto_update_supported = true;
$auto_update_filter_payload = (object) $updates[ $slug ];
} elseif ( isset( $no_updates[ $slug ] ) ) {
$auto_update_supported = true;
$auto_update_filter_payload = (object) $no_updates[ $slug ];
$auto_update_supported = false;
* Create the expected payload for the auto_update_theme filter, this is the same data
* as contained within $updates or $no_updates but used when the Theme is not known.
$auto_update_filter_payload = (object) array(
'new_version' => $theme->get( 'Version' ),
'requires' => $theme->get( 'RequiresWP' ),
'requires_php' => $theme->get( 'RequiresPHP' ),
$auto_update_forced = wp_is_auto_update_forced_for_item( 'theme', null, $auto_update_filter_payload );
$prepared_themes[ $slug ] = array(
'name' => $theme->display( 'Name' ),
'screenshot' => array( $theme->get_screenshot() ), // @todo Multiple screenshots.
'description' => $theme->display( 'Description' ),
'author' => $theme->display( 'Author', false, true ),
'authorAndUri' => $theme->display( 'Author' ),
'tags' => $theme->display( 'Tags' ),
'version' => $theme->get( 'Version' ),
'compatibleWP' => is_wp_version_compatible( $theme->get( 'RequiresWP' ) ),
'compatiblePHP' => is_php_version_compatible( $theme->get( 'RequiresPHP' ) ),
'updateResponse' => array(
'compatibleWP' => is_wp_version_compatible( $update_requires_wp ),
'compatiblePHP' => is_php_version_compatible( $update_requires_php ),
'active' => $slug === $current_theme,
'hasUpdate' => isset( $updates[ $slug ] ),
'hasPackage' => isset( $updates[ $slug ] ) && ! empty( $updates[ $slug ]['package'] ),
'update' => get_theme_update_available( $theme ),
'enabled' => $auto_update || $auto_update_forced,
'supported' => $auto_update_supported,
'forced' => $auto_update_forced,
'activate' => current_user_can( 'switch_themes' ) ? wp_nonce_url( admin_url( 'themes.php?action=activate&stylesheet=' . $encoded_slug ), 'switch-theme_' . $slug ) : null,
'customize' => $customize_action,
'delete' => ( ! is_multisite() && current_user_can( 'delete_themes' ) ) ? wp_nonce_url( admin_url( 'themes.php?action=delete&stylesheet=' . $encoded_slug ), 'delete-theme_' . $slug ) : null,
'autoupdate' => wp_is_auto_update_enabled_for_type( 'theme' ) && ! is_multisite() && current_user_can( 'update_themes' )
? wp_nonce_url( admin_url( 'themes.php?action=' . $auto_update_action . '&stylesheet=' . $encoded_slug ), 'updates' )
'blockTheme' => $theme->is_block_theme(),
// Remove 'delete' action if theme has an active child.
if ( ! empty( $parents ) && array_key_exists( $current_theme, $parents ) ) {
unset( $prepared_themes[ $parents[ $current_theme ] ]['actions']['delete'] );
* Filters the themes prepared for JavaScript, for themes.php.
* Could be useful for changing the order, which is by name by default.
* @param array $prepared_themes Array of theme data.
$prepared_themes = apply_filters( 'wp_prepare_themes_for_js', $prepared_themes );
$prepared_themes = array_values( $prepared_themes );
return array_filter( $prepared_themes );
* Prints JS templates for the theme-browsing UI in the Customizer.
function customize_themes_print_templates() {
<script type="text/html" id="tmpl-customize-themes-details-view">
<div class="theme-backdrop"></div>
<div class="theme-wrap wp-clearfix" role="document">
<div class="theme-header">
<button type="button" class="left dashicons dashicons-no"><span class="screen-reader-text">
/* translators: Hidden accessibility text. */
_e( 'Show previous theme' );
<button type="button" class="right dashicons dashicons-no"><span class="screen-reader-text">
/* translators: Hidden accessibility text. */
<button type="button" class="close dashicons dashicons-no"><span class="screen-reader-text">
/* translators: Hidden accessibility text. */
_e( 'Close details dialog' );
<div class="theme-about wp-clearfix">
<div class="theme-screenshots">
<# if ( data.screenshot && data.screenshot[0] ) { #>
<div class="screenshot"><img src="{{ data.screenshot[0] }}?ver={{ data.version }}" alt="" /></div>
<div class="screenshot blank"></div>
<# if ( data.active ) { #>
<span class="current-label"><?php _e( 'Active Theme' ); ?></span>
<h2 class="theme-name">{{{ data.name }}}<span class="theme-version">
/* translators: %s: Theme version. */
printf( __( 'Version: %s' ), '{{ data.version }}' );
<h3 class="theme-author">
/* translators: %s: Theme author link. */
printf( __( 'By %s' ), '{{{ data.authorAndUri }}}' );
<# if ( data.stars && 0 != data.num_ratings ) { #>
<div class="theme-rating">
<a class="num-ratings" target="_blank" href="{{ data.reviews_url }}">
'%1$s <span class="screen-reader-text">%2$s</span>',
/* translators: %s: Number of ratings. */
sprintf( __( '(%s ratings)' ), '{{ data.num_ratings }}' ),
/* translators: Hidden accessibility text. */
__( '(opens in a new tab)' )
<# if ( data.hasUpdate ) { #>
<# if ( data.updateResponse.compatibleWP && data.updateResponse.compatiblePHP ) { #>
<div class="notice notice-warning notice-alt notice-large" data-slug="{{ data.id }}">
<h3 class="notice-title"><?php _e( 'Update Available' ); ?></h3>
<div class="notice notice-error notice-alt notice-large" data-slug="{{ data.id }}">
<h3 class="notice-title"><?php _e( 'Update Incompatible' ); ?></h3>
<# if ( ! data.updateResponse.compatibleWP && ! data.updateResponse.compatiblePHP ) { #>
/* translators: %s: Theme name. */
__( 'There is a new version of %s available, but it does not work with your versions of WordPress and PHP.' ),
if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
/* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
self_admin_url( 'update-core.php' ),
esc_url( wp_get_update_php_url() )
wp_update_php_annotation( '</p><p><em>', '</em>' );
} elseif ( current_user_can( 'update_core' ) ) {
/* translators: %s: URL to WordPress Updates screen. */
' ' . __( '<a href="%s">Please update WordPress</a>.' ),
self_admin_url( 'update-core.php' )
} elseif ( current_user_can( 'update_php' ) ) {
/* translators: %s: URL to Update PHP page. */
' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
esc_url( wp_get_update_php_url() )
wp_update_php_annotation( '</p><p><em>', '</em>' );
<# } else if ( ! data.updateResponse.compatibleWP ) { #>
/* translators: %s: Theme name. */
__( 'There is a new version of %s available, but it does not work with your version of WordPress.' ),
if ( current_user_can( 'update_core' ) ) {
/* translators: %s: URL to WordPress Updates screen. */
' ' . __( '<a href="%s">Please update WordPress</a>.' ),
self_admin_url( 'update-core.php' )
<# } else if ( ! data.updateResponse.compatiblePHP ) { #>
/* translators: %s: Theme name. */
__( 'There is a new version of %s available, but it does not work with your version of PHP.' ),
if ( current_user_can( 'update_php' ) ) {
/* translators: %s: URL to Update PHP page. */
' ' . __( '<a href="%s">Learn more about updating PHP</a>.' ),
esc_url( wp_get_update_php_url() )
wp_update_php_annotation( '</p><p><em>', '</em>' );
<# if ( data.parent ) { #>
/* translators: %s: Theme name. */
__( 'This is a child theme of %s.' ),
'<strong>{{{ data.parent }}}</strong>'
<# if ( ! data.compatibleWP || ! data.compatiblePHP ) { #>
<div class="notice notice-error notice-alt notice-large"><p>
<# if ( ! data.compatibleWP && ! data.compatiblePHP ) { #>
_e( 'This theme does not work with your versions of WordPress and PHP.' );
if ( current_user_can( 'update_core' ) && current_user_can( 'update_php' ) ) {
/* translators: 1: URL to WordPress Updates screen, 2: URL to Update PHP page. */
' ' . __( '<a href="%1$s">Please update WordPress</a>, and then <a href="%2$s">learn more about updating PHP</a>.' ),
self_admin_url( 'update-core.php' ),
esc_url( wp_get_update_php_url() )
wp_update_php_annotation( '</p><p><em>', '</em>' );
} elseif ( current_user_can( 'update_core' ) ) {
/* translators: %s: URL to WordPress Updates screen. */
' ' . __( '<a href="%s">Please update WordPress</a>.' ),
self_admin_url( 'update-core.php' )