WP_Customize_Setting::value PHP Method

value() public method

Fetch the value of the setting.
Since: 3.4.0
public value ( ) : mixed
return mixed The value.
    public function value()
    {
        $id_base = $this->id_data['base'];
        $is_core_type = 'option' === $this->type || 'theme_mod' === $this->type;
        if (!$is_core_type && !$this->is_multidimensional_aggregated) {
            // Use post value if previewed and a post value is present.
            if ($this->is_previewed) {
                $value = $this->post_value(null);
                if (null !== $value) {
                    return $value;
                }
            }
            $value = $this->get_root_value($this->default);
            /**
             * Filters a Customize setting value not handled as a theme_mod or option.
             *
             * The dynamic portion of the hook name, `$id_base`, refers to
             * the base slug of the setting name, initialized from `$this->id_data['base']`.
             *
             * For settings handled as theme_mods or options, see those corresponding
             * functions for available hooks.
             *
             * @since 3.4.0
             * @since 4.6.0 Added the `$this` setting instance as the second parameter.
             *
             * @param mixed                $default The setting default value. Default empty.
             * @param WP_Customize_Setting $this    The setting instance.
             */
            $value = apply_filters("customize_value_{$id_base}", $value, $this);
        } elseif ($this->is_multidimensional_aggregated) {
            $root_value = self::$aggregated_multidimensionals[$this->type][$id_base]['root_value'];
            $value = $this->multidimensional_get($root_value, $this->id_data['keys'], $this->default);
            // Ensure that the post value is used if the setting is previewed, since preview filters aren't applying on cached $root_value.
            if ($this->is_previewed) {
                $value = $this->post_value($value);
            }
        } else {
            $value = $this->get_root_value($this->default);
        }
        return $value;
    }

Usage Example

 /**
  * @see WP_Customize_Widget_Setting::value()
  */
 function test_value()
 {
     $id_base = 'categories';
     $sample_data = $this->get_sample_widget_instance_data($id_base);
     $old_setting = new \WP_Customize_Setting($this->customize_manager, $sample_data['setting_id'], array('type' => 'option'));
     $args = $this->customize_manager->widgets->get_setting_args($sample_data['setting_id']);
     $new_setting = new WP_Customize_Widget_Setting($this->customize_manager, $sample_data['setting_id'], $args);
     $this->assertEquals($sample_data['instance'], $old_setting->value());
     $this->assertEquals($old_setting->value(), $new_setting->value());
 }
All Usage Examples Of WP_Customize_Setting::value