The customize_save_{$this->id_data[‘base’]} WordPress action fires when the WP_Customize_Setting::save() method is called. The dynamic portion of the hook name, $this->id_data['base'], refers to the base slug of the setting name.
Usage
add_action( 'customize_save_example_setting', 'my_custom_function' );
function my_custom_function( $this ) {
// Your custom code here
}
Parameters
$this(WP_Customize_Setting): WP_Customize_Setting instance.
More information
See WordPress Developer Resources: customize_save_{$this->id_data[‘base’]}
Examples
Change site title after saving
Update the site title after saving the ‘blogname’ setting in the Customizer:
add_action( 'customize_save_blogname', 'update_site_title' );
function update_site_title( $this ) {
// Update site title
update_option( 'blogname', 'My Custom Site Title' );
}
Log when a theme color is saved
Log the time when the ‘header_textcolor’ setting is saved:
add_action( 'customize_save_header_textcolor', 'log_color_change' );
function log_color_change( $this ) {
// Log the timestamp
update_option( 'color_change_timestamp', time() );
}
Add a prefix to saved tagline
Add a prefix to the ‘blogdescription’ setting after it’s saved:
add_action( 'customize_save_blogdescription', 'add_prefix_to_tagline' );
function add_prefix_to_tagline( $this ) {
// Get current tagline
$current_tagline = get_option( 'blogdescription' );
// Add prefix
update_option( 'blogdescription', 'My Site: ' . $current_tagline );
}
Send an email when custom logo is updated
Send an email notification when the ‘custom_logo’ setting is updated:
add_action( 'customize_save_custom_logo', 'email_on_logo_change' );
function email_on_logo_change( $this ) {
// Send an email notification
wp_mail( '[email protected]', 'Logo Updated', 'The custom logo has been updated.' );
}
Reset custom CSS when the theme is changed
Reset the ‘custom_css’ setting when the ‘stylesheet’ setting is updated:
add_action( 'customize_save_stylesheet', 'reset_custom_css' );
function reset_custom_css( $this ) {
// Reset custom CSS
update_option( 'custom_css', '' );
}
Log changes to a custom background color setting
When the background color setting is changed, this example logs the new value.
add_action('customize_save_background_color', 'log_background_color_changes', 10, 1);
function log_background_color_changes($wp_customize_setting) {
$new_value = $wp_customize_setting->post_value();
error_log("Background color changed to: " . $new_value);
}
Validate a custom text field setting
This example validates a custom text field setting, ensuring the text does not contain any disallowed characters.
add_action('customize_save_custom_text', 'validate_custom_text', 10, 1);
function validate_custom_text($wp_customize_setting) {
$new_value = $wp_customize_setting->post_value();
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=+¬-]/', $new_value)) {
$wp_customize_setting->manager->add_setting_error(new WP_Error('invalid_text', 'Invalid characters in custom text.'));
}
}
Send an email when a specific setting is changed
This example sends an email to the admin when a specific setting is changed.
add_action('customize_save_custom_setting', 'email_on_custom_setting_change', 10, 1);
function email_on_custom_setting_change($wp_customize_setting) {
$new_value = $wp_customize_setting->post_value();
$old_value = $wp_customize_setting->value();
if ($new_value !== $old_value) {
wp_mail(get_option('admin_email'), 'Custom setting changed', 'The custom setting has been changed.');
}
}
Update a cache when the site title is changed
This example updates a cache when the site title is changed.
add_action('customize_save_blogname', 'update_site_title_cache', 10, 1);
function update_site_title_cache($wp_customize_setting) {
$new_value = $wp_customize_setting->post_value();
wp_cache_set('site_title', $new_value);
}
Append a prefix to a custom footer text setting
This example appends a prefix to a custom footer text setting before saving.
add_action('customize_save_footer_text', 'prefix_footer_text', 10, 1);
function prefix_footer_text($wp_customize_setting) {
$new_value = $wp_customize_setting->post_value();
$prefixed_value = 'My Site - ' . $new_value;
$wp_customize_setting->set_root_value($prefixed_value);
}