Using WordPress ‘customize_value_{$this->id_data[‘base’]}’ PHP filter

The customize_value_{$this->id_data[‘base’]} WordPress PHP filter allows you to filter a Customize setting value that is not handled as a theme_mod or option.

Usage

add_filter('customize_value_example_setting', 'my_custom_function', 10, 1);
function my_custom_function($default) {
  // your custom code here
  return $default;
}

Parameters

  • $default (mixed): The setting default value. Default is empty.

More information

See WordPress Developer Resources: customize_value_{$this->id_data[‘base’]}

Examples

Change the default value for a custom setting

This code snippet changes the default value for a custom setting called ‘example_setting’.

add_filter('customize_value_example_setting', 'change_example_setting_default', 10, 1);
function change_example_setting_default($default) {
  $default = 'New default value';
  return $default;
}

Modify a custom setting value based on user role

This code snippet modifies the value of a custom setting called ‘example_setting’ based on the user’s role.

add_filter('customize_value_example_setting', 'modify_setting_based_on_user_role', 10, 1);
function modify_setting_based_on_user_role($default) {
  if (current_user_can('editor')) {
    $default = 'Value for Editor';
  } elseif (current_user_can('subscriber')) {
    $default = 'Value for Subscriber';
  }
  return $default;
}

Update a custom setting value using another setting’s value

This code snippet updates the value of a custom setting called ‘example_setting’ based on the value of another setting.

add_filter('customize_value_example_setting', 'update_setting_based_on_another_setting', 10, 1);
function update_setting_based_on_another_setting($default) {
  $another_setting_value = get_theme_mod('another_setting', '');
  if ($another_setting_value === 'some_value') {
    $default = 'Updated value';
  }
  return $default;
}

Modify a custom setting value using a custom function

This code snippet modifies the value of a custom setting called ‘example_setting’ using a custom function.

add_filter('customize_value_example_setting', 'modify_setting_using_custom_function', 10, 1);
function modify_setting_using_custom_function($default) {
  $new_value = custom_function($default);
  return $new_value;
}

Append text to a custom setting value

This code snippet appends a text string to a custom setting called ‘example_setting’.

add_filter('customize_value_example_setting', 'append_text_to_setting', 10, 1);
function append_text_to_setting($default) {
  $default .= ' - Appended text';
  return $default;
}