WP_Customize_Manager::post_value() WordPress Method

The WP_Customize_Manager::post_value() method is used to retrieve the value of a setting from the $_POST array. This is useful when you need to save a setting's value before it is sanitized.

WP_Customize_Manager::post_value( WP_Customize_Setting $setting, mixed $default_value = null ) #

Returns the sanitized value for a given setting from the current customized state.


Description

The name "post_value" is a carry-over from when the customized state was exclusively sourced from $_POST['customized']. Nevertheless, the value returned will come from the current changeset post and from the incoming post data.

Top ↑

See also


Top ↑

Parameters

$setting

(WP_Customize_Setting)(Required)A WP_Customize_Setting derived object.

$default_value

(mixed)(Optional)Value returned if $setting has no post value (added in 4.2.0) or the post value is invalid (added in 4.6.0).

Default value: null


Top ↑

Return

(string|mixed) Sanitized value or the $default_value provided.


Top ↑

Source

File: wp-includes/class-wp-customize-manager.php

	public function post_value( $setting, $default_value = null ) {
		$post_values = $this->unsanitized_post_values();
		if ( ! array_key_exists( $setting->id, $post_values ) ) {
			return $default_value;
		}

		$value = $post_values[ $setting->id ];
		$valid = $setting->validate( $value );
		if ( is_wp_error( $valid ) ) {
			return $default_value;
		}

		$value = $setting->sanitize( $value );
		if ( is_null( $value ) || is_wp_error( $value ) ) {
			return $default_value;
		}

		return $value;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.6.0$default_value is now returned early when the setting post value is invalid.
4.1.1Introduced the $default_value parameter.
3.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More