wp_ajax_inline_save_tax()
云策文档标注
概述
wp_ajax_inline_save_tax() 是一个 WordPress AJAX 处理函数,用于通过 Quick Edit 快速编辑功能保存分类法术语。它验证请求、检查权限并更新术语数据,最后输出更新后的行内容。
关键要点
- 函数通过 AJAX 处理 Quick Edit 保存操作,确保非ce验证和用户权限检查。
- 使用 check_ajax_referer() 验证 AJAX 请求,防止外部攻击。
- 通过 current_user_can('edit_term', $id) 检查当前用户是否有编辑术语的权限。
- 调用 wp_update_term() 更新术语数据,并处理可能的错误。
- 更新成功后,使用 WP_Terms_List_Table::single_row() 生成更新后的行内容并输出。
- 函数在失败或错误时调用 wp_die() 终止执行并显示错误信息。
代码示例
function wp_ajax_inline_save_tax() {
check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
$taxonomy = sanitize_key( $_POST['taxonomy'] );
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! $taxonomy_object ) {
wp_die( 0 );
}
if ( ! isset( $_POST['tax_ID'] ) || ! (int) $_POST['tax_ID'] ) {
wp_die( -1 );
}
$id = (int) $_POST['tax_ID'];
if ( ! current_user_can( 'edit_term', $id ) ) {
wp_die( -1 );
}
$wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => 'edit-' . $taxonomy ) );
$tag = get_term( $id, $taxonomy );
$_POST['description'] = $tag->description;
$updated = wp_update_term( $id, $taxonomy, $_POST );
if ( $updated && ! is_wp_error( $updated ) ) {
$tag = get_term( $updated['term_id'], $taxonomy );
if ( ! $tag || is_wp_error( $tag ) ) {
if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
wp_die( $tag->get_error_message() );
}
wp_die( __( 'Item not updated.' ) );
}
} else {
if ( is_wp_error( $updated ) && $updated->get_error_message() ) {
wp_die( $updated->get_error_message() );
}
wp_die( __( 'Item not updated.' ) );
}
$level = 0;
$parent = $tag->parent;
while ( $parent > 0 ) {
$parent_tag = get_term( $parent, $taxonomy );
$parent = $parent_tag->parent;
++$level;
}
$wp_list_table->single_row( $tag, $level );
wp_die();
}注意事项
- 函数依赖于 $_POST 数据,包括 'taxonomy' 和 'tax_ID',需确保前端正确传递这些参数。
- 错误处理使用 wp_die(),在开发时应注意调试信息可能被隐藏。
- 权限检查基于 current_user_can('edit_term', $id),确保用户角色配置正确。
- 函数自 WordPress 3.1.0 版本引入,使用时需考虑版本兼容性。
原文内容
Handles Quick Edit saving for a term via AJAX.
Source
function wp_ajax_inline_save_tax() {
check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' );
$taxonomy = sanitize_key( $_POST['taxonomy'] );
$taxonomy_object = get_taxonomy( $taxonomy );
if ( ! $taxonomy_object ) {
wp_die( 0 );
}
if ( ! isset( $_POST['tax_ID'] ) || ! (int) $_POST['tax_ID'] ) {
wp_die( -1 );
}
$id = (int) $_POST['tax_ID'];
if ( ! current_user_can( 'edit_term', $id ) ) {
wp_die( -1 );
}
$wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => 'edit-' . $taxonomy ) );
$tag = get_term( $id, $taxonomy );
$_POST['description'] = $tag->description;
$updated = wp_update_term( $id, $taxonomy, $_POST );
if ( $updated && ! is_wp_error( $updated ) ) {
$tag = get_term( $updated['term_id'], $taxonomy );
if ( ! $tag || is_wp_error( $tag ) ) {
if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
wp_die( $tag->get_error_message() );
}
wp_die( __( 'Item not updated.' ) );
}
} else {
if ( is_wp_error( $updated ) && $updated->get_error_message() ) {
wp_die( $updated->get_error_message() );
}
wp_die( __( 'Item not updated.' ) );
}
$level = 0;
$parent = $tag->parent;
while ( $parent > 0 ) {
$parent_tag = get_term( $parent, $taxonomy );
$parent = $parent_tag->parent;
++$level;
}
$wp_list_table->single_row( $tag, $level );
wp_die();
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |