WP_Customize_Setting::update PHP Method

update() protected method

Save the value of the setting, using the related API.
Since: 3.4.0
protected update ( mixed $value ) : boolean
$value mixed The value to update.
return boolean The result of saving the value.
    protected function update($value)
    {
        $id_base = $this->id_data['base'];
        if ('option' === $this->type || 'theme_mod' === $this->type) {
            if (!$this->is_multidimensional_aggregated) {
                return $this->set_root_value($value);
            } else {
                $root = self::$aggregated_multidimensionals[$this->type][$id_base]['root_value'];
                $root = $this->multidimensional_replace($root, $this->id_data['keys'], $value);
                self::$aggregated_multidimensionals[$this->type][$id_base]['root_value'] = $root;
                return $this->set_root_value($root);
            }
        } else {
            /**
             * Fires when the WP_Customize_Setting::update() method is called for settings
             * not handled as theme_mods or options.
             *
             * The dynamic portion of the hook name, `$this->type`, refers to the type of setting.
             *
             * @since 3.4.0
             *
             * @param mixed                $value Value of the setting.
             * @param WP_Customize_Setting $this  WP_Customize_Setting instance.
             */
            do_action("customize_update_{$this->type}", $value, $this);
            return has_action("customize_update_{$this->type}");
        }
    }

Usage Example

Example #1
0
 /**
  * Overwrites the `update()` method so we can save some extra data.
  *
  * @since  3.0.0
  * @access public
  * @param  string  $value
  * @return string
  */
 protected function update($value)
 {
     if ($value) {
         $post_id = attachment_url_to_postid($value);
         if ($post_id) {
             $image = wp_get_attachment_image_src($post_id);
             if ($image) {
                 // Set up a custom array of data to save.
                 $data = array('url' => esc_url_raw($image[0]), 'width' => absint($image[1]), 'height' => absint($image[2]), 'id' => absint($post_id));
                 set_theme_mod("{$this->id_data['base']}_data", $data);
             }
         }
     }
     // No media? Remove the data mod.
     if (empty($value) || empty($post_id) || empty($image)) {
         remove_theme_mod("{$this->id_data['base']}_data");
     }
     // Let's send this back up and let the parent class do its thing.
     return parent::update($value);
 }