Title: wp_ajax_query_themes
Published: April 25, 2014
Last modified: February 24, 2026

---

# wp_ajax_query_themes()

## In this article

 * [Source](https://developer.wordpress.org/reference/functions/wp_ajax_query_themes/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_ajax_query_themes/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_query_themes/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_ajax_query_themes/?output_format=md#wp--skip-link--target)

Handles getting themes from [themes_api()](https://developer.wordpress.org/reference/functions/themes_api/)
via AJAX.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_ajax_query_themes/?output_format=md#source)󠁿

    ```php
    function wp_ajax_query_themes() {
    	global $themes_allowedtags, $theme_field_defaults;

    	if ( ! current_user_can( 'install_themes' ) ) {
    		wp_send_json_error();
    	}

    	$args = wp_parse_args(
    		wp_unslash( $_REQUEST['request'] ),
    		array(
    			'per_page' => 20,
    			'fields'   => array_merge(
    				(array) $theme_field_defaults,
    				array(
    					'reviews_url' => true, // Explicitly request the reviews URL to be linked from the Add Themes screen.
    				)
    			),
    		)
    	);

    	if ( isset( $args['browse'] ) && 'favorites' === $args['browse'] && ! isset( $args['user'] ) ) {
    		$user = get_user_option( 'wporg_favorites' );
    		if ( $user ) {
    			$args['user'] = $user;
    		}
    	}

    	$old_filter = isset( $args['browse'] ) ? $args['browse'] : 'search';

    	/** This filter is documented in wp-admin/includes/class-wp-theme-install-list-table.php */
    	$args = apply_filters( 'install_themes_table_api_args_' . $old_filter, $args );

    	$api = themes_api( 'query_themes', $args );

    	if ( is_wp_error( $api ) ) {
    		wp_send_json_error();
    	}

    	$update_php = network_admin_url( 'update.php?action=install-theme' );

    	$installed_themes = search_theme_directories();

    	if ( false === $installed_themes ) {
    		$installed_themes = array();
    	}

    	foreach ( $installed_themes as $theme_slug => $theme_data ) {
    		// Ignore child themes.
    		if ( str_contains( $theme_slug, '/' ) ) {
    			unset( $installed_themes[ $theme_slug ] );
    		}
    	}

    	foreach ( $api->themes as &$theme ) {
    		$theme->install_url = add_query_arg(
    			array(
    				'theme'    => $theme->slug,
    				'_wpnonce' => wp_create_nonce( 'install-theme_' . $theme->slug ),
    			),
    			$update_php
    		);

    		if ( current_user_can( 'switch_themes' ) ) {
    			if ( is_multisite() ) {
    				$theme->activate_url = add_query_arg(
    					array(
    						'action'   => 'enable',
    						'_wpnonce' => wp_create_nonce( 'enable-theme_' . $theme->slug ),
    						'theme'    => $theme->slug,
    					),
    					network_admin_url( 'themes.php' )
    				);
    			} else {
    				$theme->activate_url = add_query_arg(
    					array(
    						'action'     => 'activate',
    						'_wpnonce'   => wp_create_nonce( 'switch-theme_' . $theme->slug ),
    						'stylesheet' => $theme->slug,
    					),
    					admin_url( 'themes.php' )
    				);
    			}
    		}

    		$is_theme_installed = array_key_exists( $theme->slug, $installed_themes );

    		// We only care about installed themes.
    		$theme->block_theme = $is_theme_installed && wp_get_theme( $theme->slug )->is_block_theme();

    		if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
    			$customize_url = $theme->block_theme ? admin_url( 'site-editor.php' ) : wp_customize_url( $theme->slug );

    			$theme->customize_url = add_query_arg(
    				array(
    					'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ),
    				),
    				$customize_url
    			);
    		}

    		$theme->name        = wp_kses( $theme->name, $themes_allowedtags );
    		$theme->author      = wp_kses( $theme->author['display_name'], $themes_allowedtags );
    		$theme->version     = wp_kses( $theme->version, $themes_allowedtags );
    		$theme->description = wp_kses( $theme->description, $themes_allowedtags );

    		$theme->stars = wp_star_rating(
    			array(
    				'rating' => $theme->rating,
    				'type'   => 'percent',
    				'number' => $theme->num_ratings,
    				'echo'   => false,
    			)
    		);

    		$theme->num_ratings    = number_format_i18n( $theme->num_ratings );
    		$theme->preview_url    = set_url_scheme( $theme->preview_url );
    		$theme->compatible_wp  = is_wp_version_compatible( $theme->requires );
    		$theme->compatible_php = is_php_version_compatible( $theme->requires_php );
    	}

    	wp_send_json_success( $api );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/ajax-actions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/ajax-actions.php#L3616)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/ajax-actions.php#L3616-L3737)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_ajax_query_themes/?output_format=md#related)󠁿

| Uses | Description | 
| [is_wp_version_compatible()](https://developer.wordpress.org/reference/functions/is_wp_version_compatible/)`wp-includes/functions.php` |

Checks compatibility with the current WordPress version.

  | 
| [is_php_version_compatible()](https://developer.wordpress.org/reference/functions/is_php_version_compatible/)`wp-includes/functions.php` |

Checks compatibility with the current PHP version.

  | 
| [themes_api()](https://developer.wordpress.org/reference/functions/themes_api/)`wp-admin/includes/theme.php` |

Retrieves theme installer pages from the WordPress.org Themes API.

  | 
| [wp_star_rating()](https://developer.wordpress.org/reference/functions/wp_star_rating/)`wp-admin/includes/template.php` |

Outputs a HTML element with a star rating for a given rating.

  | 
| [wp_customize_url()](https://developer.wordpress.org/reference/functions/wp_customize_url/)`wp-includes/theme.php` |

Returns a URL to load the Customizer.

  | 
| [search_theme_directories()](https://developer.wordpress.org/reference/functions/search_theme_directories/)`wp-includes/theme.php` |

Searches all registered theme directories for complete and valid themes.

  | 
| [wp_kses()](https://developer.wordpress.org/reference/functions/wp_kses/)`wp-includes/kses.php` |

Filters text content and strips out disallowed HTML.

  | 
| [set_url_scheme()](https://developer.wordpress.org/reference/functions/set_url_scheme/)`wp-includes/link-template.php` |

Sets the scheme for a URL.

  | 
| [network_admin_url()](https://developer.wordpress.org/reference/functions/network_admin_url/)`wp-includes/link-template.php` |

Retrieves the URL to the admin area for the network.

  | 
| [get_user_option()](https://developer.wordpress.org/reference/functions/get_user_option/)`wp-includes/user.php` |

Retrieves user option that can be either per Site or per Network.

  | 
| [current_user_can()](https://developer.wordpress.org/reference/functions/current_user_can/)`wp-includes/capabilities.php` |

Returns whether the current user has the specified capability.

  | 
| [wp_get_theme()](https://developer.wordpress.org/reference/functions/wp_get_theme/)`wp-includes/theme.php` |

Gets a [WP_Theme](https://developer.wordpress.org/reference/classes/wp_theme/) object for a theme.

  | 
| [wp_unslash()](https://developer.wordpress.org/reference/functions/wp_unslash/)`wp-includes/formatting.php` |

Removes slashes from a string or recursively removes slashes from strings within an array.

  | 
| [wp_create_nonce()](https://developer.wordpress.org/reference/functions/wp_create_nonce/)`wp-includes/pluggable.php` |

Creates a cryptographic token tied to a specific action, user, user session, and window of time.

  | 
| [is_multisite()](https://developer.wordpress.org/reference/functions/is_multisite/)`wp-includes/load.php` |

Determines whether Multisite is enabled.

  | 
| [wp_send_json_error()](https://developer.wordpress.org/reference/functions/wp_send_json_error/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating failure.

  | 
| [wp_parse_args()](https://developer.wordpress.org/reference/functions/wp_parse_args/)`wp-includes/functions.php` |

Merges user defined arguments into defaults array.

  | 
| [wp_send_json_success()](https://developer.wordpress.org/reference/functions/wp_send_json_success/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating success.

  | 
| [add_query_arg()](https://developer.wordpress.org/reference/functions/add_query_arg/)`wp-includes/functions.php` |

Retrieves a modified URL query string.

  | 
| [number_format_i18n()](https://developer.wordpress.org/reference/functions/number_format_i18n/)`wp-includes/functions.php` |

Converts float number to format based on the locale.

  | 
| [admin_url()](https://developer.wordpress.org/reference/functions/admin_url/)`wp-includes/link-template.php` |

Retrieves the URL to the admin area for the current site.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  |

[Show 18 more](https://developer.wordpress.org/reference/functions/wp_ajax_query_themes/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_ajax_query_themes/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_query_themes/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.9.0](https://developer.wordpress.org/reference/since/3.9.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_ajax_query_themes%2F)
before being able to contribute a note or feedback.