WP_Customize_Manager::post_value PHP Method

post_value() public method

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.
See also: WP_REST_Server::dispatch()
See also: WP_Rest_Request::sanitize_params()
See also: WP_Rest_Request::has_valid_params()
Since: 3.4.0
Since: 4.1.1 Introduced the `$default` parameter.
Since: 4.6.0 `$default` is now returned early when the setting post value is invalid.
public post_value ( WP_Customize_Setting $setting, mixed $default = null ) : string | mixed
$setting WP_Customize_Setting A WP_Customize_Setting derived object.
$default mixed Value returned $setting has no post value (added in 4.2.0) or the post value is invalid (added in 4.6.0).
return string | mixed $post_value Sanitized value or the $default provided.
    public function post_value($setting, $default = null)
    {
        $post_values = $this->unsanitized_post_values();
        if (!array_key_exists($setting->id, $post_values)) {
            return $default;
        }
        $value = $post_values[$setting->id];
        $valid = $setting->validate($value);
        if (is_wp_error($valid)) {
            return $default;
        }
        $value = $setting->sanitize($value);
        if (is_null($value) || is_wp_error($value)) {
            return $default;
        }
        return $value;
    }

Usage Example

 /**
  * Test the WP_Customize_Manager::post_value() method to make sure that the validation and sanitization are done in the right order.
  *
  * @ticket 37247
  */
 function test_post_value_validation_sanitization_order()
 {
     $default_value = '0';
     $setting = $this->manager->add_setting('numeric', array('validate_callback' => array($this, 'filter_customize_validate_numeric'), 'sanitize_callback' => array($this, 'filter_customize_sanitize_numeric')));
     $this->assertEquals($default_value, $this->manager->post_value($setting, $default_value));
     $this->assertEquals($default_value, $setting->post_value($default_value));
     $post_value = '42';
     $this->manager->set_post_value('numeric', $post_value);
     $this->assertEquals($post_value, $this->manager->post_value($setting, $default_value));
     $this->assertEquals($post_value, $setting->post_value($default_value));
 }
All Usage Examples Of WP_Customize_Manager::post_value
WP_Customize_Manager