Using WordPress ‘customize_save_{$id_base}’ PHP action

The customize_save_{$id_base} WordPress PHP action fires when the WP_Customize_Setting::save() method is called. The dynamic portion, $id_base, refers to the base slug of the setting name.

Usage

add_action('customize_save_my_setting', 'my_custom_function');
function my_custom_function($setting) {
    // Your custom code here

    return $setting;
}

Parameters

  • $setting (WP_Customize_Setting): The WP_Customize_Setting instance.

More information

See WordPress Developer Resources: customize_save_{$id_base}

Examples

Update a color setting

Update a custom color setting when it’s saved in the Customizer.

add_action('customize_save_my_color_setting', 'update_my_color_setting');
function update_my_color_setting($setting) {
    // Update color setting
    $color = $setting->value();
    set_theme_mod('my_color_setting', $color);
}

Log setting changes

Log changes to a specific setting when it’s saved in the Customizer.

add_action('customize_save_my_text_setting', 'log_my_text_setting_changes');
function log_my_text_setting_changes($setting) {
    // Log setting changes
    $old_value = get_theme_mod('my_text_setting');
    $new_value = $setting->value();

    if ($old_value !== $new_value) {
        error_log('My Text Setting changed from ' . $old_value . ' to ' . $new_value);
    }
}

Update custom CSS based on a setting

Update custom CSS when a specific setting is saved in the Customizer.

add_action('customize_save_my_font_size_setting', 'update_my_font_size_css');
function update_my_font_size_css($setting) {
    // Update custom CSS based on the font size setting
    $font_size = $setting->value();
    $custom_css = "body { font-size: {$font_size}px; }";
    wp_add_inline_style('my-theme-style', $custom_css);
}

Send an email when a setting is updated

Send an email to the site administrator when a specific setting is saved in the Customizer.

add_action('customize_save_my_email_setting', 'send_email_on_setting_update');
function send_email_on_setting_update($setting) {
    // Send an email when the setting is updated
    $new_value = $setting->value();
    $to = get_option('admin_email');
    $subject = 'My Email Setting Updated';
    $message = 'The email setting was updated to: ' . $new_value;

    wp_mail($to, $subject, $message);
}

Clear cache on setting update

Clear cache for a specific page when a setting is saved in the Customizer.

add_action('customize_save_my_cache_setting', 'clear_cache_on_setting_update');
function clear_cache_on_setting_update($setting) {
    // Clear cache for the specific page
    $page_id = $setting->value();
    if (function_exists('wp_cache_clear_cache')) {
        wp_cache_clear_cache($page_id);
    }
}