函数文档

get_term_to_edit()

💡 云策文档标注

概述

get_term_to_edit() 函数用于在编辑前对术语进行清理,确保数据安全。它基于 sanitize_term() 实现,简化了编辑上下文中的术语处理流程。

关键要点

  • 函数接受术语 ID 或对象和分类法名称作为参数,返回清理后的术语或错误信息。
  • 如果 $term 不是对象,函数返回空字符串;如果 get_term() 返回 WP_Error,则直接返回该错误。
  • 核心功能依赖于 sanitize_term() 进行字段清理,适用于 WordPress 开发中的术语管理场景。

代码示例

function get_term_to_edit( $id, $taxonomy ) {
    $term = get_term( $id, $taxonomy );

    if ( is_wp_error( $term ) ) {
        return $term;
    }

    if ( ! is_object( $term ) ) {
        return '';
    }

    return sanitize_term( $term, $taxonomy, 'edit' );
}

📄 原文内容

Sanitizes term for editing.

Description

Return value is sanitize_term() and usage is for sanitizing the term for editing. Function is for contextual and simplicity.

Parameters

$idint|objectrequired
Term ID or object.
$taxonomystringrequired
Taxonomy name.

Return

string|int|null|WP_Error Will return empty string if $term is not an object.

Source

function get_term_to_edit( $id, $taxonomy ) {
	$term = get_term( $id, $taxonomy );

	if ( is_wp_error( $term ) ) {
		return $term;
	}

	if ( ! is_object( $term ) ) {
		return '';
	}

	return sanitize_term( $term, $taxonomy, 'edit' );
}

Changelog

Version Description
2.3.0 Introduced.