wp_ajax_add_tag()
云策文档标注
概述
wp_ajax_add_tag() 是一个 WordPress AJAX 处理函数,用于通过 AJAX 请求添加标签或其他分类法术语。它验证请求、检查用户权限、插入术语并返回响应。
关键要点
- 函数通过 AJAX 处理添加标签或分类法术语的请求,使用 check_ajax_referer() 验证安全性。
- 检查当前用户是否具有 edit_terms 权限,否则通过 wp_die() 终止执行。
- 使用 wp_insert_term() 插入术语,处理成功或错误情况,并通过 WP_Ajax_Response 返回结构化响应。
- 对于分层分类法,计算术语层级并生成列表表行内容,同时加载编辑标签消息。
- 返回响应包含术语数据、消息和补充信息,如 parents 和 noparents 内容。
代码示例
check_ajax_referer( 'add-tag', '_wpnonce_add-tag' );
$taxonomy = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'post_tag';
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! current_user_can( $taxonomy_object->cap->edit_terms ) ) {
wp_die( -1 );
}
$x = new WP_Ajax_Response();
$tag = wp_insert_term( $_POST['tag-name'], $taxonomy, $_POST );
if ( $tag && ! is_wp_error( $tag ) ) {
$tag = get_term( $tag['term_id'], $taxonomy );
}注意事项
- 函数默认处理 post_tag 分类法,但可通过 $_POST['taxonomy'] 指定其他分类法。
- 错误处理包括检查 WP_Error 对象,并返回相应的错误消息和代码。
- 使用 WP_Ajax_Response 类确保 AJAX 响应格式正确,便于前端处理。
- 涉及多个 WordPress 核心函数,如 get_taxonomy()、is_taxonomy_hierarchical() 和 _get_list_table(),需确保环境兼容。
原文内容
Handles adding a tag via AJAX.
Source
function wp_ajax_add_tag() {
check_ajax_referer( 'add-tag', '_wpnonce_add-tag' );
$taxonomy = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'post_tag';
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! current_user_can( $taxonomy_object->cap->edit_terms ) ) {
wp_die( -1 );
}
$x = new WP_Ajax_Response();
$tag = wp_insert_term( $_POST['tag-name'], $taxonomy, $_POST );
if ( $tag && ! is_wp_error( $tag ) ) {
$tag = get_term( $tag['term_id'], $taxonomy );
}
if ( ! $tag || is_wp_error( $tag ) ) {
$message = __( 'An error has occurred. Please reload the page and try again.' );
$error_code = 'error';
if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
$message = $tag->get_error_message();
}
if ( is_wp_error( $tag ) && $tag->get_error_code() ) {
$error_code = $tag->get_error_code();
}
$x->add(
array(
'what' => 'taxonomy',
'data' => new WP_Error( $error_code, $message ),
)
);
$x->send();
}
$wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => $_POST['screen'] ) );
$level = 0;
$noparents = '';
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
$level = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) );
ob_start();
$wp_list_table->single_row( $tag, $level );
$noparents = ob_get_clean();
}
ob_start();
$wp_list_table->single_row( $tag );
$parents = ob_get_clean();
require ABSPATH . 'wp-admin/includes/edit-tag-messages.php';
$message = '';
if ( isset( $messages[ $taxonomy_object->name ][1] ) ) {
$message = $messages[ $taxonomy_object->name ][1];
} elseif ( isset( $messages['_item'][1] ) ) {
$message = $messages['_item'][1];
}
$x->add(
array(
'what' => 'taxonomy',
'data' => $message,
'supplemental' => array(
'parents' => $parents,
'noparents' => $noparents,
'notice' => $message,
),
)
);
$x->add(
array(
'what' => 'term',
'position' => $level,
'supplemental' => (array) $tag,
)
);
$x->send();
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |