I have created a theme options page for a WordPress theme, and I need to add a Reset button to clear all the user defined settings for theme options.
I'm told that this function will do the job,
function reset_mytheme_options() {
remove_theme_mods();
}
add_action( 'after_switch_theme', 'reset_mytheme_options' );
But, don't know how to run this function on a button click.
So, will this function do what I needed ? If so, how to run it on a button click ?
As you mentioned you are using settings API then you can implement in this way.
Add button in theme option page after the submit button.
submit_button(__('Reset'), 'secondary', 'reset', false);
When you registering settings pass the validation callback
register_setting('option_group', 'options_name', 'save_theme_option');
When form is submitted by reset
button then check the request for reset and return the default settings.
Example
function save_theme_option($input) {
if (isset($_POST['reset'])) {
add_settings_error('settingName', 'SettingSlug', __('Your settings has been changed defualt setting.', 'text-domain'), 'updated');
return array('a' => 1, 'b' => 2); //Default settings
}
return $input;
}