Using WordPress ‘customize_dynamic_setting_args’ PHP filter

The customize_dynamic_setting_args WordPress PHP filter allows you to modify a dynamic setting’s constructor arguments.

Usage

add_filter('customize_dynamic_setting_args', 'my_custom_function', 10, 2);

function my_custom_function($setting_args, $setting_id) {
  // your custom code here
  return $setting_args;
}

Parameters

  • $setting_args: false|array – The arguments to the WP_Customize_Setting constructor.
  • $setting_id: string – ID for dynamic setting, usually coming from $_POST[‘customized’].

More information

See WordPress Developer Resources: customize_dynamic_setting_args

Examples

Change the transport method for a dynamic setting

Modify the transport method for a dynamic setting with a specific ID.

add_filter('customize_dynamic_setting_args', 'change_transport_method', 10, 2);

function change_transport_method($setting_args, $setting_id) {
  if ('my_dynamic_setting' === $setting_id) {
    $setting_args = array(
      'transport' => 'postMessage',
    );
  }
  return $setting_args;
}

Add a default value for a dynamic setting

Set a default value for a dynamic setting with a specific ID.

add_filter('customize_dynamic_setting_args', 'add_default_value', 10, 2);

function add_default_value($setting_args, $setting_id) {
  if ('my_dynamic_setting' === $setting_id) {
    $setting_args = array(
      'default' => 'My default value',
    );
  }
  return $setting_args;
}

Change the capability required for a dynamic setting

Modify the capability required for a dynamic setting with a specific ID.

add_filter('customize_dynamic_setting_args', 'change_capability', 10, 2);

function change_capability($setting_args, $setting_id) {
  if ('my_dynamic_setting' === $setting_id) {
    $setting_args = array(
      'capability' => 'manage_options',
    );
  }
  return $setting_args;
}

Add sanitization callback for a dynamic setting

Add a sanitization callback for a dynamic setting with a specific ID.

add_filter('customize_dynamic_setting_args', 'add_sanitization_callback', 10, 2);

function add_sanitization_callback($setting_args, $setting_id) {
  if ('my_dynamic_setting' === $setting_id) {
    $setting_args = array(
      'sanitize_callback' => 'sanitize_text_field',
    );
  }
  return $setting_args;
}

Add a validation callback for a dynamic setting

Add a validation callback for a dynamic setting with a specific ID.

add_filter('customize_dynamic_setting_args', 'add_validation_callback', 10, 2);

function add_validation_callback($setting_args, $setting_id) {
  if ('my_dynamic_setting' === $setting_id) {
    $setting_args = array(
      'validate_callback' => 'my_custom_validation',
    );
  }
  return $setting_args;
}

function my_custom_validation($validity, $value) {
  if ('invalid_value' === $value) {
    $validity->add('invalid_value', 'The value is not allowed.');
  }
  return $validity;
}