Using WordPress ‘customize_dynamic_setting_class’ PHP filter

The customize_dynamic_setting_class WordPress PHP filter allows non-statically created settings to be constructed with a custom WP_Customize_Setting subclass.

Usage

add_filter('customize_dynamic_setting_class', 'your_function_name', 10, 3);

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

Parameters

  • $setting_class: string, WP_Customize_Setting or a subclass.
  • $setting_id: string, ID for dynamic setting, usually coming from $_POST[‘customized’].
  • $setting_args: array, WP_Customize_Setting or a subclass.

More information

See WordPress Developer Resources: customize_dynamic_setting_class

Examples

Use custom class for specific setting id

Create a custom class for a specific setting id and apply it using the filter.

class My_Custom_Setting extends WP_Customize_Setting {
  // Custom functionality here
}

add_filter('customize_dynamic_setting_class', 'use_custom_setting_class', 10, 3);

function use_custom_setting_class($setting_class, $setting_id, $setting_args) {
  if ('my_custom_setting_id' === $setting_id) {
    $setting_class = 'My_Custom_Setting';
  }
  return $setting_class;
}

Change setting class based on setting type

Apply a different custom class for different types of settings.

class Custom_Text_Setting extends WP_Customize_Setting {}
class Custom_Color_Setting extends WP_Customize_Setting {}

add_filter('customize_dynamic_setting_class', 'change_setting_class_based_on_type', 10, 3);

function change_setting_class_based_on_type($setting_class, $setting_id, $setting_args) {
  if (isset($setting_args['type'])) {
    if ('text' === $setting_args['type']) {
      $setting_class = 'Custom_Text_Setting';
    } elseif ('color' === $setting_args['type']) {
      $setting_class = 'Custom_Color_Setting';
    }
  }
  return $setting_class;
}

Add a prefix to setting ids

Add a prefix to all setting ids using the filter.

add_filter('customize_dynamic_setting_class', 'add_prefix_to_setting_ids', 10, 3);

function add_prefix_to_setting_ids($setting_class, $setting_id, $setting_args) {
  $prefix = 'my_prefix_';
  $setting_id = $prefix . $setting_id;
  return $setting_class;
}

Modify setting class based on post type

Change the setting class based on the post type of the associated setting.

class Custom_Post_Type_Setting extends WP_Customize_Setting {}

add_filter('customize_dynamic_setting_class', 'modify_class_based_on_post_type', 10, 3);

function modify_class_based_on_post_type($setting_class, $setting_id, $setting_args) {
  if (isset($setting_args['post_type']) && 'custom_post_type' === $setting_args['post_type']) {
    $setting_class = 'Custom_Post_Type_Setting';
  }
  return $setting_class;
}

Conditionally disable a setting

Disable a specific setting based on a condition.

add_filter('customize_dynamic_setting_class', 'conditionally_disable_setting', 10, 3);

function conditionally_disable_setting($setting_class, $setting_id, $setting_args) {
  if ('disable_this_setting_id' === $setting_id && some_condition()) {
$setting_class = false; // Disable the setting by setting the class to false
}
return $setting_class;
}

function some_condition() {
// Your custom condition logic here
return true; // Replace with your actual condition
}