sanitize_post

函数
sanitize_post ( $post, $context = 'display' )
参数
  • (object|WP_Post|array) $post The post object or array
    Required:
  • (string) $context Optional. How to sanitize post fields. Accepts 'raw', 'edit', 'db', 'display', 'attribute', or 'js'. Default 'display'.
    Required:
    Default: 'display'
返回值
  • (object|WP_Post|array) The now sanitized post object or array (will be the same type as `$post`).
相关
  • sanitize_post_field()
定义位置
相关方法
sanitize_post_fieldsanitize_optionsanitize_termsanitize_usersanitize_meta
引入
2.3.0
弃用
-

sanitize_post: 这是一个WordPress的函数,它对一个文章对象进行净化。它用于验证和净化文章的数据,如标题和内容: 这个函数有一个参数,就是要净化的文章对象。

对每个文章字段进行净化。

如果上下文是”raw”,那么文章对象或数组将得到最小的整数字段的净化处理。

function sanitize_post( $post, $context = 'display' ) {
	if ( is_object( $post ) ) {
		// Check if post already filtered for this context.
		if ( isset( $post->filter ) && $context == $post->filter ) {
			return $post;
		}
		if ( ! isset( $post->ID ) ) {
			$post->ID = 0;
		}
		foreach ( array_keys( get_object_vars( $post ) ) as $field ) {
			$post->$field = sanitize_post_field( $field, $post->$field, $post->ID, $context );
		}
		$post->filter = $context;
	} elseif ( is_array( $post ) ) {
		// Check if post already filtered for this context.
		if ( isset( $post['filter'] ) && $context == $post['filter'] ) {
			return $post;
		}
		if ( ! isset( $post['ID'] ) ) {
			$post['ID'] = 0;
		}
		foreach ( array_keys( $post ) as $field ) {
			$post[ $field ] = sanitize_post_field( $field, $post[ $field ], $post['ID'], $context );
		}
		$post['filter'] = $context;
	}
	return $post;
}

常见问题

FAQs
查看更多 >