函数文档

_wp_get_allowed_postdata()

💡 云策文档标注

概述

_wp_get_allowed_postdata() 函数用于过滤 POST 数据,仅返回允许的字段。它处理传入的数组或 WP_Error 对象,默认使用 $_POST 超全局变量。

关键要点

  • 函数参数 $post_data 可选,可以是数组、WP_Error 或 null,默认值为 null,此时使用 $_POST。
  • 如果 $post_data 是 WP_Error,函数直接返回该错误对象。
  • 函数通过 array_diff_key 移除 'meta_input'、'file' 和 'guid' 字段,返回过滤后的数组。
  • 该函数在 WordPress 5.0.1 版本中引入,主要用于处理与文章相关的 POST 数据操作。

代码示例

function _wp_get_allowed_postdata( $post_data = null ) {
    if ( empty( $post_data ) ) {
        $post_data = $_POST;
    }

    // Pass through errors.
    if ( is_wp_error( $post_data ) ) {
        return $post_data;
    }

    return array_diff_key( $post_data, array_flip( array( 'meta_input', 'file', 'guid' ) ) );
}

注意事项

  • 函数不验证字段内容,仅移除特定键;确保传入数据已进行适当清理。
  • 相关函数如 wp_create_post_autosave() 和 edit_post() 依赖此函数处理 POST 数据。

📄 原文内容

Returns only allowed post data fields.

Parameters

$post_dataarray|WP_Error|nulloptional
The array of post data to process, or an error object.
Defaults to the $_POST superglobal.

Default:null

Return

array|WP_Error Array of post data on success, WP_Error on failure.

Source

function _wp_get_allowed_postdata( $post_data = null ) {
	if ( empty( $post_data ) ) {
		$post_data = $_POST;
	}

	// Pass through errors.
	if ( is_wp_error( $post_data ) ) {
		return $post_data;
	}

	return array_diff_key( $post_data, array_flip( array( 'meta_input', 'file', 'guid' ) ) );
}

Changelog

Version Description
5.0.1 Introduced.