wppaste
WordPress

wp_ajax_ajax_tag_search()

Since
3.1.0
Source
wp-admin/includes/ajax-actions.php:106
Handles tag search via AJAX.

Compatibility

WordPress
since 3.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Performance profile

How much work a call to wp_ajax_ajax_tag_search() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_terms().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
76–103

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 104.

Plugin surface
2 hooks

Third-party callbacks on 'term_search_min_chars', 'ajax_term_search_results' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_terms()called directly

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 32 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_ajax_ajax_tag_search() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
isset($value) && current_user_can() && !$search76–85sanitize_key(), get_taxonomy(), current_user_can(), wp_unslash(), _x(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
isset($value) && current_user_can() && !$search && $term_search_min_chars !== 0 && !$term_search_min_chars77–83sanitize_key(), get_taxonomy(), current_user_can(), wp_unslash(), _x(), apply_filters(), taxonomy(), apply_filters(), wp_die()
isset($value) && !current_user_can() && !$search79–88sanitize_key(), get_taxonomy(), current_user_can(), wp_die(), wp_unslash(), _x(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
isset($value) && current_user_can() && !$search79–88sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_unslash(), _x(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
!isset($value) && current_user_can() && !$search79–88wp_die(), sanitize_key(), get_taxonomy(), current_user_can(), wp_unslash(), _x(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
isset($value) && !current_user_can() && !$search && $term_search_min_chars !== 0 && !$term_search_min_chars80–86sanitize_key(), get_taxonomy(), current_user_can(), wp_die(), wp_unslash(), _x(), apply_filters(), taxonomy(), apply_filters(), wp_die()
isset($value) && current_user_can() && !$search && $term_search_min_chars !== 0 && !$term_search_min_chars80–86sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_unslash(), _x(), apply_filters(), taxonomy(), apply_filters(), wp_die()
!isset($value) && current_user_can() && !$search && $term_search_min_chars !== 0 && !$term_search_min_chars80–86wp_die(), sanitize_key(), get_taxonomy(), current_user_can(), wp_unslash(), _x(), apply_filters(), taxonomy(), apply_filters(), wp_die()
isset($value) && !current_user_can() && !$search82–91sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_die(), wp_unslash(), _x(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
!isset($value) && !current_user_can() && !$search82–91wp_die(), sanitize_key(), get_taxonomy(), current_user_can(), wp_die(), wp_unslash(), _x(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
!isset($value) && current_user_can() && !$search82–91wp_die(), sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_unslash(), _x(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
isset($value) && !current_user_can() && !$search && $term_search_min_chars !== 0 && !$term_search_min_chars83–89sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_die(), wp_unslash(), _x(), apply_filters(), taxonomy(), apply_filters(), wp_die()
20 further outcomes, up to 101 instructions
!isset($value) && !current_user_can() && !$search && $term_search_min_chars !== 0 && !$term_search_min_chars83–89wp_die(), sanitize_key(), get_taxonomy(), current_user_can(), wp_die(), wp_unslash(), _x(), apply_filters(), taxonomy(), apply_filters(), wp_die()
!isset($value) && current_user_can() && !$search && $term_search_min_chars !== 0 && !$term_search_min_chars83–89wp_die(), sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_unslash(), _x(), apply_filters(), taxonomy(), apply_filters(), wp_die()
isset($value) && current_user_can() && $search85–94sanitize_key(), get_taxonomy(), current_user_can(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
!isset($value) && !current_user_can() && !$search85–94wp_die(), sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_die(), wp_unslash(), _x(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
isset($value) && current_user_can() && $search && $term_search_min_chars !== 0 && !$term_search_min_chars86–92sanitize_key(), get_taxonomy(), current_user_can(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), taxonomy(), apply_filters(), wp_die()
!isset($value) && !current_user_can() && !$search && $term_search_min_chars !== 0 && !$term_search_min_chars86–92wp_die(), sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_die(), wp_unslash(), _x(), apply_filters(), taxonomy(), apply_filters(), wp_die()
isset($value) && !current_user_can() && $search88–97sanitize_key(), get_taxonomy(), current_user_can(), wp_die(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
isset($value) && current_user_can() && $search88–97sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
!isset($value) && current_user_can() && $search88–97wp_die(), sanitize_key(), get_taxonomy(), current_user_can(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
isset($value) && !current_user_can() && $search && $term_search_min_chars !== 0 && !$term_search_min_chars89–95sanitize_key(), get_taxonomy(), current_user_can(), wp_die(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), taxonomy(), apply_filters(), wp_die()
isset($value) && current_user_can() && $search && $term_search_min_chars !== 0 && !$term_search_min_chars89–95sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), taxonomy(), apply_filters(), wp_die()
!isset($value) && current_user_can() && $search && $term_search_min_chars !== 0 && !$term_search_min_chars89–95wp_die(), sanitize_key(), get_taxonomy(), current_user_can(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), taxonomy(), apply_filters(), wp_die()
isset($value) && !current_user_can() && $search91–100sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_die(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
!isset($value) && !current_user_can() && $search91–100wp_die(), sanitize_key(), get_taxonomy(), current_user_can(), wp_die(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
!isset($value) && current_user_can() && $search91–100wp_die(), sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
isset($value) && !current_user_can() && $search && $term_search_min_chars !== 0 && !$term_search_min_chars92–98sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_die(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), taxonomy(), apply_filters(), wp_die()
!isset($value) && !current_user_can() && $search && $term_search_min_chars !== 0 && !$term_search_min_chars92–98wp_die(), sanitize_key(), get_taxonomy(), current_user_can(), wp_die(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), taxonomy(), apply_filters(), wp_die()
!isset($value) && current_user_can() && $search && $term_search_min_chars !== 0 && !$term_search_min_chars92–98wp_die(), sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), taxonomy(), apply_filters(), wp_die()
!isset($value) && !current_user_can() && $search94–103wp_die(), sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_die(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), wp_die(), taxonomy(), apply_filters(), wp_die()
!isset($value) && !current_user_can() && $search && $term_search_min_chars !== 0 && !$term_search_min_chars95–101wp_die(), sanitize_key(), get_taxonomy(), wp_die(), current_user_can(), wp_die(), wp_unslash(), _x(), explode(), array_last(), apply_filters(), taxonomy(), apply_filters(), wp_die()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10476–1038
8.510476–1038
8.410476–103811 fewer instructions than PHP 8.3
8.311584–1148
8.211584–1148
8.111584–11481 fewer instruction than PHP 7.4
7.411684–1158

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_ajax_ajax_tag_search() runs, in this order:

  1. apply_filters( term_search_min_chars )filterline 145 (+39 into the body)

    Filters the minimum number of characters required to fire a tag search via Ajax.

  2. apply_filters( ajax_term_search_results )filterline 174 (+68 into the body)

    Filters the Ajax term search results.

Uses · 10

  • wp_die()Kills WordPress execution and displays HTML page with an error message.
  • sanitize_key()Sanitizes a string key.
  • get_taxonomy()Retrieves the taxonomy object of $taxonomy.
  • current_user_can()Returns whether the current user has the specified capability.
  • wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
  • _x()Retrieves translated string with gettext context.
  • str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
  • array_last()Polyfill for `array_last()` function added in PHP 8.5.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • get_terms()Retrieves the terms in a given taxonomy or list of taxonomies.

Source code

function wp_ajax_ajax_tag_search() {	if ( ! isset( $_GET['tax'] ) ) {		wp_die( 0 );	} 	$taxonomy        = sanitize_key( $_GET['tax'] );	$taxonomy_object = get_taxonomy( $taxonomy ); 	if ( ! $taxonomy_object ) {		wp_die( 0 );	} 	if ( ! current_user_can( $taxonomy_object->cap->assign_terms ) ) {		wp_die( -1 );	} 	$search = wp_unslash( $_GET['q'] ); 	$comma = _x( ',', 'tag delimiter' );	if ( ',' !== $comma ) {		$search = str_replace( $comma, ',', $search );	} 	if ( str_contains( $search, ',' ) ) {		$search = explode( ',', $search );		$search = array_last( $search );	} 	$search = trim( $search ); 	/**	 * Filters the minimum number of characters required to fire a tag search via Ajax.	 *	 * @since 4.0.0	 *	 * @param int         $characters      The minimum number of characters required. Default 2.	 * @param WP_Taxonomy $taxonomy_object The taxonomy object.	 * @param string      $search          The search term.	 */	$term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $taxonomy_object, $search ); 	/*	 * Require $term_search_min_chars chars for matching (default: 2)	 * ensure it's a non-negative, non-zero integer.	 */	if ( ( 0 === $term_search_min_chars ) || ( strlen( $search ) < $term_search_min_chars ) ) {		wp_die();	} 	$results = get_terms(		array(			'taxonomy'   => $taxonomy,			'name__like' => $search,			'fields'     => 'names',			'hide_empty' => false,			'number'     => isset( $_GET['number'] ) ? (int) $_GET['number'] : 0,		)	); 	/**	 * Filters the Ajax term search results.	 *	 * @since 6.1.0	 *	 * @param string[]    $results         Array of term names.	 * @param WP_Taxonomy $taxonomy_object The taxonomy object.	 * @param string      $search          The search term.	 */	$results = apply_filters( 'ajax_term_search_results', $results, $taxonomy_object, $search ); 	echo implode( "\n", $results );	wp_die();}

Changelog

Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-admin/includes/ajax-actions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.