钩子文档

create_{$taxonomy}

💡 云策文档标注

概述

create_{$taxonomy} 是一个 WordPress 动态 Hook,在特定分类法下创建新术语后触发。它允许开发者在术语创建时执行自定义操作,例如更新缓存或发送通知。

关键要点

  • 这是一个动态 Hook,$taxonomy 部分替换为分类法的 slug,例如 create_category 或 create_post_tag。
  • Hook 在 wp_insert_term() 函数调用后触发,提供术语 ID、术语分类法 ID 和参数数组。
  • 参数 $args 包含从 wp_insert_term() 传递的详细信息,如别名、描述、父术语 ID 和 slug。
  • 自 WordPress 6.1.0 版本起,$args 参数被添加,增强了 Hook 的功能性。

代码示例

add_action('create_category', 'my_custom_function', 10, 3);
function my_custom_function($term_id, $tt_id, $args) {
    // 自定义逻辑,例如记录日志或更新相关数据
    error_log('New category created with ID: ' . $term_id);
}

注意事项

  • 确保 Hook 名称正确,根据实际分类法动态替换 $taxonomy。
  • 参数 $args 在 6.1.0 版本前不可用,开发时需考虑版本兼容性。
  • 避免在 Hook 回调中执行耗时操作,以免影响性能。

📄 原文内容

Fires after a new term is created for a specific taxonomy.

Description

The dynamic portion of the hook name, $taxonomy, refers to the slug of the taxonomy the term was created for.

Possible hook names include:

  • create_category
  • create_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_of string
    Slug of the term to make this term an alias of.
    Accepts a term slug.
  • description string
    The term description.
  • parent int
    The id of the parent term. Default 0.
  • slug string
    The term slug to use.

Source

do_action( "create_{$taxonomy}", $term_id, $tt_id, $args );

Changelog

Version Description
6.1.0 The $args parameter was added.
2.3.0 Introduced.