sanitize_term

函数
sanitize_term ( $term, $taxonomy, $context = 'display' )
参数
  • (array|object) $term The term to check.
    Required:
  • (string) $taxonomy The taxonomy name to use.
    Required:
  • (string) $context Optional. Context in which to sanitize the term. Accepts 'raw', 'edit', 'db', 'display', 'rss', 'attribute', or 'js'. Default 'display'.
    Required:
    Default: 'display'
返回值
  • (array|object) Term with all fields sanitized.
定义位置
相关方法
sanitize_usersanitize_emailsanitize_titlesanitize_term_fieldsanitize_category
引入
2.3.0
弃用
-

sanitize_term: 这是一个WordPress的函数,可以对一个术语对象进行净化。它用于验证和净化术语的数据,如名称和描述: 这个函数有一个参数,就是要进行净化的术语对象。

对所有术语字段进行净化。

依靠sanitize_term_field()来对术语进行净化。不同的是,这个函数将对***所有字段进行净化。上下文是基于sanitize_term_field()。

`$term`应该是一个数组或一个对象。

function sanitize_term( $term, $taxonomy, $context = 'display' ) {
	$fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' );

	$do_object = is_object( $term );

	$term_id = $do_object ? $term->term_id : ( isset( $term['term_id'] ) ? $term['term_id'] : 0 );

	foreach ( (array) $fields as $field ) {
		if ( $do_object ) {
			if ( isset( $term->$field ) ) {
				$term->$field = sanitize_term_field( $field, $term->$field, $term_id, $taxonomy, $context );
			}
		} else {
			if ( isset( $term[ $field ] ) ) {
				$term[ $field ] = sanitize_term_field( $field, $term[ $field ], $term_id, $taxonomy, $context );
			}
		}
	}

	if ( $do_object ) {
		$term->filter = $context;
	} else {
		$term['filter'] = $context;
	}

	return $term;
}

常见问题

FAQs
查看更多 >