钩子文档

edit_terms

💡 云策文档标注

概述

edit_terms 是一个 WordPress 动作钩子,在术语更新到数据库之前触发,允许插件或主题在更新前执行自定义代码。

关键要点

  • 触发时机:在术语通过 wp_update_term() 更新到数据库之前立即执行。
  • 参数:$term_id(术语ID)、$taxonomy(分类法slug)、$args(传递给 wp_update_term() 的参数数组)。
  • 用途:可用于在术语编辑前进行数据验证、日志记录或阻止更新操作。
  • 版本历史:自 WordPress 2.9.0 引入,6.1.0 版本添加了 $args 参数。

代码示例

add_action( 'edit_terms', 'do_something_or_stop_update', 10, 2 );

function do_something_or_stop_update( $term_id, $taxonomy ){
    //do something before update OR
    //redirect back to the edit page to disable updates (remember to exit)
}

注意事项

  • 参数 $args 包含更新术语的详细信息,如 alias_of、description、parent 和 slug。
  • 在钩子函数中,可以通过重定向并退出(exit)来阻止术语更新。
  • 相关函数包括 wp_update_term() 和 wp_insert_term(),用于术语的更新和插入操作。

📄 原文内容

Fires immediately before the given terms are edited.

Parameters

$term_idint
Term 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.

More Information

The edit_terms action is used to hook into code before a term is updated in the database.

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

Source

do_action( 'edit_terms', $term_id, $taxonomy, $args );

Changelog

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

User Contributed Notes