WP_Customize_Manager::add_dynamic_settings() WordPress Method

The WP_Customize_Manager::add_dynamic_settings() method allows you to add settings to the Customizer at runtime, which is handy if you need to add settings based on certain conditions. For example, you could use this method to add a setting only when the current user is an administrator: $wp_customize->add_dynamic_settings( array( 'some_setting' => array( 'capability' => 'manage_options', 'default' => 'foo', ), ) ); Or, you could use it to add a setting only when a certain plugin is active: $wp_customize->add_dynamic_settings( array( 'some_setting' => array( 'active_callback' => 'is_plugin_active', 'default' => 'foo', ), ) );

WP_Customize_Manager::add_dynamic_settings( array $setting_ids ) #

Registers any dynamically-created settings, such as those from $_POST[‘customized’] that have no corresponding setting created.


Description

This is a mechanism to "wake up" settings that have been dynamically created on the front end and have been sent to WordPress in $_POST['customized']. When WP loads, the dynamically-created settings then will get created and previewed even though they are not directly created statically with code.


Top ↑

Parameters

$setting_ids

(array)(Required)The setting IDs to add.


Top ↑

Return

(array) The WP_Customize_Setting objects added.


Top ↑

Source

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

	public function add_dynamic_settings( $setting_ids ) {
		$new_settings = array();
		foreach ( $setting_ids as $setting_id ) {
			// Skip settings already created.
			if ( $this->get_setting( $setting_id ) ) {
				continue;
			}

			$setting_args  = false;
			$setting_class = 'WP_Customize_Setting';

			/**
			 * Filters a dynamic setting's constructor args.
			 *
			 * For a dynamic setting to be registered, this filter must be employed
			 * to override the default false value with an array of args to pass to
			 * the WP_Customize_Setting constructor.
			 *
			 * @since 4.2.0
			 *
			 * @param false|array $setting_args The arguments to the WP_Customize_Setting constructor.
			 * @param string      $setting_id   ID for dynamic setting, usually coming from `$_POST['customized']`.
			 */
			$setting_args = apply_filters( 'customize_dynamic_setting_args', $setting_args, $setting_id );
			if ( false === $setting_args ) {
				continue;
			}

			/**
			 * Allow non-statically created settings to be constructed with custom WP_Customize_Setting subclass.
			 *
			 * @since 4.2.0
			 *
			 * @param string $setting_class WP_Customize_Setting or a subclass.
			 * @param string $setting_id    ID for dynamic setting, usually coming from `$_POST['customized']`.
			 * @param array  $setting_args  WP_Customize_Setting or a subclass.
			 */
			$setting_class = apply_filters( 'customize_dynamic_setting_class', $setting_class, $setting_id, $setting_args );

			$setting = new $setting_class( $this, $setting_id, $setting_args );

			$this->add_setting( $setting );
			$new_settings[] = $setting;
		}
		return $new_settings;
	}


Top ↑

Changelog

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