I have been attempting to create a theme reset button for the theme customizer that automatically removes all of the theme_mod settings. I have tried multiple ways to do this and have never been able to get it to work. As seen here.
After multiple failed attempts using the remove_theme_mods approach I begin to wonder if that is my problem other than the ajax and javascript being faulty and not properly binding the button.
Since I save all defaults into one big array so when my theme is installed it automatically has values populated on the theme customizer page, and the theme has a specific look… I was thinking I could try a different approach to instead remove the theme settings I just over ride them, maybe with a custom control? Possibly by somehow assigning these default values to all settings? I really hope someone can help me get this going. If you have any ideas I would appreciate it very very much.
Here is an example of how I assign the default values for the theme:
function newtheme_get_theme_mods() {
$defaults = array(
'newtheme_responsive' => true,
'newtheme_hero_title' => 'Beautiful and Elegant',
'newtheme_hero_description' => 'The best theme in the world',
'newtheme_header_logo' => '/wp-content/themes/newtheme/img/logo.png',
'newtheme_header_background_color' => 'rgba(35,35,35, 0.9)'
return $defaults;
}
There is a function called
remove_theme_mods
that will kill all theme mods. Beware, however, that this will not only remove the mods that you have defined in your theme using the customizer API. It will also target other mods, such as the menu locations. Widget positions are currently defined as options, not as mods, but this may change in the future. WordPress is likely to move more stuff from options to mods, makingremove_theme_mods
all the more dangerous to use.The best approach is to arrange all mods that belong to the theme proper in an array. You can then loop through that array and remove the individual mods with
remove_theme_mod
.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.)So in your theme setting page do something like this:
this will removes all of the theme_mod settings