Using WordPress ‘customize_value_{$id_base}’ PHP filter

The customize_value_{$id_base} WordPress PHP filter allows you to modify the value of a Customize setting that isn’t managed as a theme_mod or option.

Usage

add_filter('customize_value_my_setting', 'my_custom_function', 10, 2);
function my_custom_function($default_value, $setting) {
    // Your custom code here

    return $default_value;
}

Parameters

  • $default_value (mixed): The setting default value. Default empty.
  • $setting (WP_Customize_Setting): The setting instance.

More information

See WordPress Developer Resources: customize_value_{$id_base}

Examples

Modify a custom setting value

This example changes the value of a custom setting called my_custom_setting.

add_filter('customize_value_my_custom_setting', 'change_my_custom_setting', 10, 2);
function change_my_custom_setting($default_value, $setting) {
    $new_value = 'New custom value';

    return $new_value;
}

Set a default value for a custom setting

This example sets a default value for a custom setting called my_background_color.

add_filter('customize_value_my_background_color', 'set_default_background_color', 10, 2);
function set_default_background_color($default_value, $setting) {
    $default_value = '#ffffff';

    return $default_value;
}

Validate a custom setting value

This example validates the value of a custom setting called my_age_range to make sure it’s a number between 18 and 99.

add_filter('customize_value_my_age_range', 'validate_age_range', 10, 2);
function validate_age_range($default_value, $setting) {
    if ($default_value >= 18 && $default_value <= 99) {
        return $default_value;
    }

    return 18;
}

Modify a custom setting value based on another setting

This example modifies the value of a custom setting called my_custom_logo based on the value of another setting called my_logo_position.

add_filter('customize_value_my_custom_logo', 'change_logo_based_on_position', 10, 2);
function change_logo_based_on_position($default_value, $setting) {
    $logo_position = get_theme_mod('my_logo_position');

    if ($logo_position == 'left') {
        $default_value = 'Left logo image';
    } elseif ($logo_position == 'right') {
        $default_value = 'Right logo image';
    }

    return $default_value;
}

Modify a custom setting value using a custom function

This example modifies the value of a custom setting called my_custom_text by running it through a custom function that shortens the text.

add_filter('customize_value_my_custom_text', 'shorten_custom_text', 10, 2);
function shorten_custom_text($default_value, $setting) {
    $shortened_text = my_custom_shorten_text_function($default_value);

    return $shortened_text;
}