Using WordPress ‘customize_post_value_set’ PHP action

The customize_post_value_set WordPress PHP action announces when any setting’s unsanitized post value has been set, which is useful for WP_Customize_Setting instances to watch in order to update a cached previewed value.

Usage

add_action('customize_post_value_set', 'your_custom_function', 10, 3);

function your_custom_function($setting_id, $value, $manager) {
    // your custom code here
}

Parameters

  • $setting_id (string) – The setting ID.
  • $value (mixed) – The unsanitized setting post value.
  • $manager (WP_Customize_Manager) – The WP_Customize_Manager instance.

More information

See WordPress Developer Resources: customize_post_value_set

Examples

Log setting changes

Log setting changes to a custom log file.

function log_setting_changes($setting_id, $value, $manager) {
    error_log("Setting {$setting_id} changed to: {$value}", 3, "/path/to/your/log/file.log");
}
add_action('customize_post_value_set', 'log_setting_changes', 10, 3);

Apply custom sanitization

Apply custom sanitization to the site title before saving.

function custom_sanitize_site_title($setting_id, $value, $manager) {
    if ($setting_id == 'blogname') {
        $value = strtoupper($value);
    }
    $manager->set_post_value($setting_id, $value);
}
add_action('customize_post_value_set', 'custom_sanitize_site_title', 10, 3);

Update a related setting based on the change of another setting.

function update_related_setting($setting_id, $value, $manager) {
    if ($setting_id == 'main_color') {
        $new_value = complementary_color($value);
        $manager->set_post_value('secondary_color', $new_value);
    }
}
add_action('customize_post_value_set', 'update_related_setting', 10, 3);

Set a default value for a custom setting

Set a default value for a custom setting if it’s empty.

function set_default_value($setting_id, $value, $manager) {
    if ($setting_id == 'custom_setting' && empty($value)) {
        $manager->set_post_value($setting_id, 'default_value');
    }
}
add_action('customize_post_value_set', 'set_default_value', 10, 3);

Send an email when a specific setting changes

Send an email notification to the admin when the site’s tagline is updated.

function notify_admin_on_tagline_update($setting_id, $value, $manager) {
    if ($setting_id == 'blogdescription') {
        $subject = 'Site tagline updated';
        $message = "The site tagline has been updated to: {$value}";
        wp_mail(get_option('admin_email'), $subject, $message);
    }
}
add_action('customize_post_value_set', 'notify_admin_on_tagline_update', 10, 3);