WP_Customize_Setting::__construct() WordPress Method

The WP_Customize_Setting::__construct() method is used to setup a new customizer setting. It takes two parameters: the customizer manager instance and an array of args to customize the new setting. The args can include: -an 'id' (required) -a 'type' (optional) -a 'capability' (optional) -a 'default' (optional) -a 'transport' (optional) -a 'sanitize_callback' (optional) -a 'sanitize_js_callback' (optional) The 'id' is the only required arg, and it should be a unique identifier for the setting. The 'type' arg can be used to set the data type for the setting (string, integer, boolean, etc), and the 'capability' arg can be used to set the required capability for accessing the setting. The 'default' arg can be used to set the default value for the setting, and the 'transport' arg can be used to set how the setting's value will be passed to the front-end (postMessage, refresh, or none). The 'sanitize_callback' arg can be used to set a callback function for sanitizing the setting's value before it is saved, and the 'sanitize_js_callback' arg can be used to set a callback function for sanitizing the setting's value before it is passed to the front-end.

WP_Customize_Setting::__construct( WP_Customize_Manager $manager, string $id, array $args = array() ) #

Constructor.


Description

Any supplied $args override class property defaults.


Top ↑

Parameters

$manager

(WP_Customize_Manager)(Required)Customizer bootstrap instance.

$id

(string)(Required)A specific ID of the setting. Can be a theme mod or option name.

$args

(array)(Optional)Array of properties for the new Setting object.

  • 'type'
    (string) Type of the setting. Default 'theme_mod'.
  • 'capability'
    (string) Capability required for the setting. Default 'edit_theme_options'
  • 'theme_supports'
    (string|string[]) Theme features required to support the panel. Default is none.
  • 'default'
    (string) Default value for the setting. Default is empty string.
  • 'transport'
    (string) Options for rendering the live preview of changes in Customizer. Using 'refresh' makes the change visible by reloading the whole preview. Using 'postMessage' allows a custom JavaScript to handle live changes. Default is 'refresh'.
  • 'validate_callback'
    (callable) Server-side validation callback for the setting's value.
  • 'sanitize_callback'
    (callable) Callback to filter a Customize setting value in un-slashed form.
  • 'sanitize_js_callback'
    (callable) Callback to convert a Customize PHP setting value to a value that is JSON serializable.
  • 'dirty'
    (bool) Whether or not the setting is initially dirty when created.

Default value: array()


Top ↑

Source

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

	public function __construct( $manager, $id, $args = array() ) {
		$keys = array_keys( get_object_vars( $this ) );
		foreach ( $keys as $key ) {
			if ( isset( $args[ $key ] ) ) {
				$this->$key = $args[ $key ];
			}
		}

		$this->manager = $manager;
		$this->id      = $id;

		// Parse the ID for array keys.
		$this->id_data['keys'] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
		$this->id_data['base'] = array_shift( $this->id_data['keys'] );

		// Rebuild the ID.
		$this->id = $this->id_data['base'];
		if ( ! empty( $this->id_data['keys'] ) ) {
			$this->id .= '[' . implode( '][', $this->id_data['keys'] ) . ']';
		}

		if ( $this->validate_callback ) {
			add_filter( "customize_validate_{$this->id}", $this->validate_callback, 10, 3 );
		}
		if ( $this->sanitize_callback ) {
			add_filter( "customize_sanitize_{$this->id}", $this->sanitize_callback, 10, 2 );
		}
		if ( $this->sanitize_js_callback ) {
			add_filter( "customize_sanitize_js_{$this->id}", $this->sanitize_js_callback, 10, 2 );
		}

		if ( 'option' === $this->type || 'theme_mod' === $this->type ) {
			// Other setting types can opt-in to aggregate multidimensional explicitly.
			$this->aggregate_multidimensional();

			// Allow option settings to indicate whether they should be autoloaded.
			if ( 'option' === $this->type && isset( $args['autoload'] ) ) {
				self::$aggregated_multidimensionals[ $this->type ][ $this->id_data['base'] ]['autoload'] = $args['autoload'];
			}
		}
	}


Top ↑

Changelog

Changelog
VersionDescription
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.