I have created a custom control for the theme customizer which is a simple button and label. I am going to be using it as a theme reset button that will clear the theme mod settings to their original state. Now that I have added the control and have it showing up on the customizer, I am not sure where I am supposed to add the code to reset the settings.
So far I have only created customizer settings for css and text changes. To remove the settings I will be using the remove theme mods function.
<?php remove_theme_mods() ?>
So my question is do is how exactly do I use this button to execute the remove_mods function as seen above? The documentation on that function is very minimal.
If there is another way to reset the theme mod settings to default and this is not the right approach than please chime in.
Here is the code I have created my custom button with.
function newtheme_customize_reset_control($wp_customize) {
/**
* Reset Control
*
*/
class newtheme_Customize_reset_Control extends WP_Customize_Control {
public $type = 'button';
public function render_content() {
?>
<label>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<div>
<a href="#" class="button-secondary upload"><?php _e( 'Reset Settings' ); ?></a>
</div>
</label>
<?php
}
}
}
add_action( 'customize_register', 'newtheme_customize_reset_control', 1, 1 );
In theme customizer you can register your custom javascript to wordpress theme customizer
and inside your javascript file you can do something like this
and inside ajax you can do something like this
Haaaaaa hope it helps.
The problem with using
remove_theme_mods
for showing defaults in the Customizer isI also wanted a reset button, but I chose instead to create a Preset control, and one of the presets is "defaults". This way uses a
select
, so there is no problem with the button not working (becausebind
is for value changes and buttons don't change their values).The trick is to use ajax to retrieve the chosen preset, and then loop over the values in javascript, assigning them to the settings so that those changes will trigger the refresh of the preview. My code includes filters so that child themes can add in more options and presets. And the presets can be subsets of the options available.
Here is PHP for the Preset control (just a normal
select
, but a settingless control):Here is the rest of the PHP functions.
And then just a little bit of Javascript to make the ajax request. This is queued on the
'customize_controls_enqueue_scripts'
action. (I left out the display of the error message.)