created_{$taxonomy}
云策文档标注
概述
这是一个 WordPress 钩子,在特定分类法(taxonomy)中创建新术语(term)后触发,并在术语缓存清理后执行。钩子名称包含动态部分,允许针对不同分类法定制。
关键要点
- 钩子名称:created_{$taxonomy},其中 $taxonomy 是分类法的 slug,例如 created_category 或 created_post_tag。
- 触发时机:在 wp_insert_term() 成功创建新术语并清理缓存后。
- 参数:$term_id(术语 ID)、$tt_id(术语分类法 ID)、$args(传递给 wp_insert_term() 的参数数组)。
- 用途:允许开发者在术语创建后执行自定义操作,如更新相关数据或发送通知。
代码示例
add_action('created_category', 'my_custom_function', 10, 3);
function my_custom_function($term_id, $tt_id, $args) {
// 自定义逻辑,例如记录日志或更新选项
error_log('New category created with ID: ' . $term_id);
}注意事项
- 钩子名称是动态的,需根据具体分类法替换 $taxonomy 部分。
- 参数 $args 在 WordPress 6.1.0 版本中添加,包含 alias_of、description、parent、slug 等字段。
- 确保钩子函数正确处理参数,避免因版本差异导致错误。
原文内容
Fires after a new term in a specific taxonomy is created, and after the term cache has been cleaned.
Description
The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug.
Possible hook names include:
created_categorycreated_post_tag
Parameters
$term_idint-
Term ID.
$tt_idint-
Term taxonomy ID.
$argsarray-
Arguments passed to wp_insert_term() .
More Arguments from wp_insert_term( … $args )
Array or query string of arguments for inserting 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( "created_{$taxonomy}", $term_id, $tt_id, $args );