函数文档

wp_ajax_ajax_tag_search()

💡 云策文档标注

概述

wp_ajax_ajax_tag_search() 是一个 WordPress AJAX 处理函数,用于通过 AJAX 搜索标签或其他分类法术语。它验证用户权限、处理搜索查询并返回匹配的术语名称。

关键要点

  • 函数通过 AJAX 处理标签搜索,支持自定义分类法。
  • 验证用户是否具有 assign_terms 权限,确保安全性。
  • 使用 get_terms() 查询匹配的术语,并应用 term_search_min_chars 和 ajax_term_search_results 过滤器。
  • 返回术语名称列表,以换行符分隔输出。

代码示例

// 示例调用:通过 AJAX 请求搜索标签
// 假设 tax 参数为 'post_tag',q 参数为搜索词
$taxonomy = 'post_tag';
$search = 'example';
$results = get_terms(
    array(
        'taxonomy' => $taxonomy,
        'name__like' => $search,
        'fields' => 'names',
        'hide_empty' => false,
        'number' => 10,
    )
);
// 应用过滤器并输出结果
$results = apply_filters('ajax_term_search_results', $results, get_taxonomy($taxonomy), $search);
echo implode("n", $results);

注意事项

  • 必须通过 AJAX 调用,并传递 tax 和 q 参数,否则会终止执行。
  • 搜索词需满足 term_search_min_chars 过滤器设置的最小字符数(默认 2)。
  • 使用 sanitize_key() 和 wp_unslash() 确保输入安全。
  • 输出前应用 ajax_term_search_results 过滤器,允许自定义结果。

📄 原文内容

Handles tag search via AJAX.

Source

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();
}

Hooks

apply_filters( ‘ajax_term_search_results’, string[] $results, WP_Taxonomy $taxonomy_object, string $search )

Filters the Ajax term search results.

apply_filters( ‘term_search_min_chars’, int $characters, WP_Taxonomy $taxonomy_object, string $search )

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

Changelog

Version Description
3.1.0 Introduced.