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
查看更多 >
闪电侠

(工作日 10:00 - 18:30 为您服务)

2025-12-05 19:32:59

您好,无论是售前、售后、意见建议……均可通过联系工单与我们取得联系。

您也可选择聊天工具与我们即时沟通或点击查看:

您的工单我们已经收到,我们将会尽快跟您联系!
取消
选择聊天工具: