wp_ajax_ajax_tag_search

The timeline below displays how wordpress function wp_ajax_ajax_tag_search has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Handles tag search via AJAX.
 *
 * @since 3.1.0
 */
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 = $search[count($search) - 1];
    }
    $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();
}

WordPress Version: 6.1

/**
 * Ajax handler for tag search.
 *
 * @since 3.1.0
 */
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 (false !== strpos($search, ',')) {
        $search = explode(',', $search);
        $search = $search[count($search) - 1];
    }
    $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();
}

WordPress Version: 5.6

/**
 * Ajax handler for tag search.
 *
 * @since 3.1.0
 */
function wp_ajax_ajax_tag_search()
{
    if (!isset($_GET['tax'])) {
        wp_die(0);
    }
    $taxonomy = sanitize_key($_GET['tax']);
    $tax = get_taxonomy($taxonomy);
    if (!$tax) {
        wp_die(0);
    }
    if (!current_user_can($tax->cap->assign_terms)) {
        wp_die(-1);
    }
    $s = wp_unslash($_GET['q']);
    $comma = _x(',', 'tag delimiter');
    if (',' !== $comma) {
        $s = str_replace($comma, ',', $s);
    }
    if (false !== strpos($s, ',')) {
        $s = explode(',', $s);
        $s = $s[count($s) - 1];
    }
    $s = trim($s);
    /**
     * 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 $tax        The taxonomy object.
     * @param string      $s          The search term.
     */
    $term_search_min_chars = (int) apply_filters('term_search_min_chars', 2, $tax, $s);
    /*
     * 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($s) < $term_search_min_chars) {
        wp_die();
    }
    $results = get_terms(array('taxonomy' => $taxonomy, 'name__like' => $s, 'fields' => 'names', 'hide_empty' => false));
    echo implode("\n", $results);
    wp_die();
}

WordPress Version: 5.4

/**
 * Ajax handler for tag search.
 *
 * @since 3.1.0
 */
function wp_ajax_ajax_tag_search()
{
    if (!isset($_GET['tax'])) {
        wp_die(0);
    }
    $taxonomy = sanitize_key($_GET['tax']);
    $tax = get_taxonomy($taxonomy);
    if (!$tax) {
        wp_die(0);
    }
    if (!current_user_can($tax->cap->assign_terms)) {
        wp_die(-1);
    }
    $s = wp_unslash($_GET['q']);
    $comma = _x(',', 'tag delimiter');
    if (',' !== $comma) {
        $s = str_replace($comma, ',', $s);
    }
    if (false !== strpos($s, ',')) {
        $s = explode(',', $s);
        $s = $s[count($s) - 1];
    }
    $s = trim($s);
    /**
     * 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 $tax        The taxonomy object.
     * @param string      $s          The search term.
     */
    $term_search_min_chars = (int) apply_filters('term_search_min_chars', 2, $tax, $s);
    /*
     * 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($s) < $term_search_min_chars) {
        wp_die();
    }
    $results = get_terms(array('taxonomy' => $taxonomy, 'name__like' => $s, 'fields' => 'names', 'hide_empty' => false));
    echo join("\n", $results);
    wp_die();
}

WordPress Version: 5.3

/**
 * Ajax handler for tag search.
 *
 * @since 3.1.0
 */
function wp_ajax_ajax_tag_search()
{
    if (!isset($_GET['tax'])) {
        wp_die(0);
    }
    $taxonomy = sanitize_key($_GET['tax']);
    $tax = get_taxonomy($taxonomy);
    if (!$tax) {
        wp_die(0);
    }
    if (!current_user_can($tax->cap->assign_terms)) {
        wp_die(-1);
    }
    $s = wp_unslash($_GET['q']);
    $comma = _x(',', 'tag delimiter');
    if (',' !== $comma) {
        $s = str_replace($comma, ',', $s);
    }
    if (false !== strpos($s, ',')) {
        $s = explode(',', $s);
        $s = $s[count($s) - 1];
    }
    $s = trim($s);
    /**
     * 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 $tax        The taxonomy object.
     * @param string      $s          The search term.
     */
    $term_search_min_chars = (int) apply_filters('term_search_min_chars', 2, $tax, $s);
    /*
     * Require $term_search_min_chars chars for matching (default: 2)
     * ensure it's a non-negative, non-zero integer.
     */
    if ($term_search_min_chars == 0 || strlen($s) < $term_search_min_chars) {
        wp_die();
    }
    $results = get_terms(array('taxonomy' => $taxonomy, 'name__like' => $s, 'fields' => 'names', 'hide_empty' => false));
    echo join("\n", $results);
    wp_die();
}

WordPress Version: 4.7

/**
 * Ajax handler for tag search.
 *
 * @since 3.1.0
 */
function wp_ajax_ajax_tag_search()
{
    if (!isset($_GET['tax'])) {
        wp_die(0);
    }
    $taxonomy = sanitize_key($_GET['tax']);
    $tax = get_taxonomy($taxonomy);
    if (!$tax) {
        wp_die(0);
    }
    if (!current_user_can($tax->cap->assign_terms)) {
        wp_die(-1);
    }
    $s = wp_unslash($_GET['q']);
    $comma = _x(',', 'tag delimiter');
    if (',' !== $comma) {
        $s = str_replace($comma, ',', $s);
    }
    if (false !== strpos($s, ',')) {
        $s = explode(',', $s);
        $s = $s[count($s) - 1];
    }
    $s = trim($s);
    /**
     * 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 $tax        The taxonomy object.
     * @param string      $s          The search term.
     */
    $term_search_min_chars = (int) apply_filters('term_search_min_chars', 2, $tax, $s);
    /*
     * Require $term_search_min_chars chars for matching (default: 2)
     * ensure it's a non-negative, non-zero integer.
     */
    if ($term_search_min_chars == 0 || strlen($s) < $term_search_min_chars) {
        wp_die();
    }
    $results = get_terms($taxonomy, array('name__like' => $s, 'fields' => 'names', 'hide_empty' => false));
    echo join($results, "\n");
    wp_die();
}

WordPress Version: 4.6

/**
 * Ajax handler for tag search.
 *
 * @since 3.1.0
 */
function wp_ajax_ajax_tag_search()
{
    if (!isset($_GET['tax'])) {
        wp_die(0);
    }
    $taxonomy = sanitize_key($_GET['tax']);
    $tax = get_taxonomy($taxonomy);
    if (!$tax) {
        wp_die(0);
    }
    if (!current_user_can($tax->cap->assign_terms)) {
        wp_die(-1);
    }
    $s = wp_unslash($_GET['q']);
    $comma = _x(',', 'tag delimiter');
    if (',' !== $comma) {
        $s = str_replace($comma, ',', $s);
    }
    if (false !== strpos($s, ',')) {
        $s = explode(',', $s);
        $s = $s[count($s) - 1];
    }
    $s = trim($s);
    /**
     * 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 object $tax        The taxonomy object.
     * @param string $s          The search term.
     */
    $term_search_min_chars = (int) apply_filters('term_search_min_chars', 2, $tax, $s);
    /*
     * Require $term_search_min_chars chars for matching (default: 2)
     * ensure it's a non-negative, non-zero integer.
     */
    if ($term_search_min_chars == 0 || strlen($s) < $term_search_min_chars) {
        wp_die();
    }
    $results = get_terms($taxonomy, array('name__like' => $s, 'fields' => 'names', 'hide_empty' => false));
    echo join($results, "\n");
    wp_die();
}

WordPress Version: 4.1

/**
 * Ajax handler for tag search.
 *
 * @since 3.1.0
 */
function wp_ajax_ajax_tag_search()
{
    if (!isset($_GET['tax'])) {
        wp_die(0);
    }
    $taxonomy = sanitize_key($_GET['tax']);
    $tax = get_taxonomy($taxonomy);
    if (!$tax) {
        wp_die(0);
    }
    if (!current_user_can($tax->cap->assign_terms)) {
        wp_die(-1);
    }
    $s = wp_unslash($_GET['q']);
    $comma = _x(',', 'tag delimiter');
    if (',' !== $comma) {
        $s = str_replace($comma, ',', $s);
    }
    if (false !== strpos($s, ',')) {
        $s = explode(',', $s);
        $s = $s[count($s) - 1];
    }
    $s = trim($s);
    /**
     * Filter 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 object $tax        The taxonomy object.
     * @param string $s          The search term.
     */
    $term_search_min_chars = (int) apply_filters('term_search_min_chars', 2, $tax, $s);
    /*
     * Require $term_search_min_chars chars for matching (default: 2)
     * ensure it's a non-negative, non-zero integer.
     */
    if ($term_search_min_chars == 0 || strlen($s) < $term_search_min_chars) {
        wp_die();
    }
    $results = get_terms($taxonomy, array('name__like' => $s, 'fields' => 'names', 'hide_empty' => false));
    echo join($results, "\n");
    wp_die();
}

WordPress Version: 4.0

/**
 * Ajax handler for tag search.
 *
 * @since 3.1.0
 */
function wp_ajax_ajax_tag_search()
{
    if (isset($_GET['tax'])) {
        $taxonomy = sanitize_key($_GET['tax']);
        $tax = get_taxonomy($taxonomy);
        if (!$tax) {
            wp_die(0);
        }
        if (!current_user_can($tax->cap->assign_terms)) {
            wp_die(-1);
        }
    } else {
        wp_die(0);
    }
    $s = wp_unslash($_GET['q']);
    $comma = _x(',', 'tag delimiter');
    if (',' !== $comma) {
        $s = str_replace($comma, ',', $s);
    }
    if (false !== strpos($s, ',')) {
        $s = explode(',', $s);
        $s = $s[count($s) - 1];
    }
    $s = trim($s);
    /**
     * Filter 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 object $tax        The taxonomy object.
     * @param string $s          The search term.
     */
    $term_search_min_chars = (int) apply_filters('term_search_min_chars', 2, $tax, $s);
    /*
     * Require $term_search_min_chars chars for matching (default: 2)
     * ensure it's a non-negative, non-zero integer.
     */
    if ($term_search_min_chars == 0 || strlen($s) < $term_search_min_chars) {
        wp_die();
    }
    $results = get_terms($taxonomy, array('name__like' => $s, 'fields' => 'names', 'hide_empty' => false));
    echo join($results, "\n");
    wp_die();
}

WordPress Version: 3.7

function wp_ajax_ajax_tag_search()
{
    global $wpdb;
    if (isset($_GET['tax'])) {
        $taxonomy = sanitize_key($_GET['tax']);
        $tax = get_taxonomy($taxonomy);
        if (!$tax) {
            wp_die(0);
        }
        if (!current_user_can($tax->cap->assign_terms)) {
            wp_die(-1);
        }
    } else {
        wp_die(0);
    }
    $s = wp_unslash($_GET['q']);
    $comma = _x(',', 'tag delimiter');
    if (',' !== $comma) {
        $s = str_replace($comma, ',', $s);
    }
    if (false !== strpos($s, ',')) {
        $s = explode(',', $s);
        $s = $s[count($s) - 1];
    }
    $s = trim($s);
    if (strlen($s) < 2) {
        wp_die();
    }
    // require 2 chars for matching
    $results = get_terms($taxonomy, array('name__like' => $s, 'fields' => 'names', 'hide_empty' => false));
    echo join($results, "\n");
    wp_die();
}