钩子文档

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 = '&notice=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_category
  • edited_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_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.

More Information

The edit_$taxonomy action is used to hook into code after a custom taxonomy term is updated in the database.

A plugin (or theme) can register an action hook from the example below.

Source

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

Changelog

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

User Contributed Notes

  1. Skip to note 3 content

    Example from Codex

    taxonomy;
        
        // Setup the redirect URL.
        $tax_param = '&taxonomy;=' . $taxonomy_slug;
        $term_param = '&tag;_ID=' . $term_id;
        $notice_param = '&notice;=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;
    
    }
    ?>

  2. Skip to note 4 content

    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 = '&notice;=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;
    
    }