edited_{$taxonomy}
云策文档标注
概述
edited_{$taxonomy} 是一个 WordPress 动作钩子,在特定分类法(taxonomy)的术语(term)更新且缓存清理后触发。它允许开发者在术语更新后执行自定义代码,例如重定向或数据同步。
关键要点
- 钩子名称是动态的,基于分类法 slug,例如 edited_category 或 edited_post_tag。
- 参数包括 $term_id(术语 ID)、$tt_id(术语分类法 ID)和 $args(传递给 wp_update_term() 的参数数组)。
- 从 WordPress 6.1.0 版本开始,新增了 $args 参数,提供更多更新选项。
- 常用于插件或主题中,以响应术语更新事件,如重定向到编辑页面或更新相关数据。
代码示例
add_action( 'edited_custom_taxonomy', 'custom_term_slug_edit_success', 10, 2 );
function custom_term_slug_edit_success( $term_id, $tt_id ) {
$term = get_term( $term_id );
$tax_slug = $term->taxonomy;
$tax_param = '&taxonomy=' . $tax_slug;
$term_param = '&tag_ID=' . $term_id;
$notice_param = '¬ice=success';
$redirect_url = admin_url( 'edit-tags.php?action=edit' . $tax_param . $term_param . $notice_param );
wp_safe_redirect( $redirect_url );
exit;
}注意事项
- 确保钩子名称正确匹配分类法 slug,否则可能无法触发。
- 在函数中处理参数时,注意 $args 数组包含 alias_of、description、parent 和 slug 等字段,可用于进一步操作。
- 使用 wp_safe_redirect() 进行重定向时,确保退出脚本以避免后续代码执行。
原文内容
Fires after a term for a specific taxonomy has been updated, and the term cache has been cleaned.
Description
The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug.
Possible hook names include:
edited_categoryedited_post_tag
Parameters
$term_idint-
Term ID.
$tt_idint-
Term taxonomy ID.
$argsarray-
Arguments passed to wp_update_term() .
More Arguments from wp_update_term( … $args )
Array of arguments for updating a term.
alias_ofstringSlug of the term to make this term an alias of.
Accepts a term slug.descriptionstringThe term description.parentintThe id of the parent term. Default 0.slugstringThe term slug to use.
Source
do_action( "edited_{$taxonomy}", $term_id, $tt_id, $args );
Skip to note 3 content
Akira Tachibana
Example from Codex
taxonomy; // Setup the redirect URL. $tax_param = '&taxonomy;=' . $taxonomy_slug; $term_param = '&tag;_ID=' . $term_id; $notice_param = '¬ice;=success'; $redirect_url = admin_url( 'edit-tags.php?action=edit' . $tax_param . $tag_param . $notice_param ); // Redirect with new query strings. wp_safe_redirect( $redirect_url ); exit; } ?>Skip to note 4 content
Collins Mbaka
add_action( 'edited_custom_taxonomy', 'custom_term_slug_edit_success', 10, 2 ); /** * Custom redirect on taxonomy term update, keeps users on the term page for additional updates * * @param $term_id * @param $tt_id */ function custom_term_slug_edit_success( $term_id, $tt_id ) { // Get the taxonomy slug. $term = get_term( $term_id ); $tax_slug = $term->taxonomy; // Setup the redirect URL. $tax_param = '&taxonomy;=' . $taxonomy_slug; $term_param = '&tag;_ID=' . $term_id; $notice_param = '¬ice;=success'; $redirect_url = admin_url( 'edit-tags.php?action=edit' . $tax_param . $tag_param . $notice_param ); // Redirect with new query strings. wp_safe_redirect( $redirect_url ); exit; }