钩子文档

edit_term

💡 云策文档标注

概述

edit_term 是一个 WordPress 动作钩子,在分类术语更新后、缓存清理前触发,允许开发者执行自定义操作。它支持特定分类的钩子变体,并提供了丰富的参数信息。

关键要点

  • 触发时机:在术语更新后、缓存清理前执行。
  • 钩子变体:可使用 'edit_$taxonomy' 针对特定分类(如 'edit_category')。
  • 参数:包括 $term_id(术语ID)、$tt_id(术语分类ID)、$taxonomy(分类slug)、$args(传递给 wp_update_term() 的参数数组)。
  • 版本历史:WordPress 6.1.0 添加了 $args 参数,2.3.0 引入此钩子。
  • 相关函数:与 wp_update_term() 关联,用于更新术语。

代码示例

function wp_edit_term_example( $term_id, $tt_id, $taxonomy ) {
    // Return if it is not default taxonomy
    if ( 'category' !== $taxonomy ) {
        return;
    }

    // Get term data by term id and taxonomy
    $term = get_term( $term_id, $taxonomy );

    // Update term meta
    update_term_meta( $term_id, '_term_example_meta_key', 'term_example_meta_value' );
}

add_action( 'edit_term', 'wp_edit_term_example', 10, 3 );

📄 原文内容

Fires after a term has been updated, but before the term cache has been cleaned.

Description

The ‘edit_$taxonomy’ hook is also available for targeting a specific taxonomy.

Parameters

$term_idint
Term ID.
$tt_idint
Term taxonomy ID.
$taxonomystring
Taxonomy slug.
$argsarray
Arguments passed to wp_update_term() .

More Arguments from wp_update_term( … $args )

Array of arguments for updating 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( 'edit_term', $term_id, $tt_id, $taxonomy, $args );

Changelog

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

User Contributed Notes

  1. Skip to note 2 content

    Here is the working example for edit_term action

    function wp_edit_term_example( $term_id, $tt_id, $taxonomy ) {
        // Return if it is not default taxonomy
        if ( 'category' !== $taxonomy ) {
            return;
        }
    
        // Get term data by term id and taxonomy
        $term = get_term( $term_id, $taxonomy );
    
        // Update term meta
        update_term_meta( $term_id, '_term_example_meta_key', 'term_example_meta_value' );
    }
    
    add_action( 'edit_term', 'wp_edit_term_example', 10, 3 );