• Resolved Luke

    (@lukejanicke)


    Problem. I want to include a RESET button on a theme’s settings page. I don’t mean a button that just clears any changes just made on the form. I mean a button that resets all the theme’s settings to their defaults.

    I’m putting together a theme settings page for the theme I’m developing. This is where a few theme options can be toggled on/off and some custom items set, such as the copyright notice and social media links. I’ve got it all working. The page is at: Dashboard > Appearance > Respite Settings. And all the settings are working correctly (saving properly, able to be used in the theme’s template files).

    Note: Respite is the name of my theme.

    I have a function in my options.php file that can do perform the settings reset. I just don’t know how to link a reset button on the options page form to that function. You can see my options.php file on Github.

    https://github.com/lukejanicke/respite/blob/master/options.php

    A quick search turned up a few StackExchange threads about doing this but none had enough information to help me actually do it.

    Any help would be appreciated!

Viewing 2 replies - 1 through 2 (of 2 total)
  • nickohrn

    (@nickohrn)

    I’ve submitted a PR which should accomplish what you’re looking for: https://github.com/lukejanicke/respite/pull/1

    Thanks for putting your snippet on GitHub! Much easier to help that way. The PR contains details about what I modified / added, but if you have any other questions please feel free to let me know.

    Thread Starter Luke

    (@lukejanicke)

    That was officially my first pull request and merge! I’m pretty new to Github and a very casual WordPress theme developer (and not at all a programming expert). I merged your pull request. It works perfectly. The only thing I might add now is a delete_option so that during development when I’m adding all sorts of different settings and then discarding them, the RESET will fully clear/clean the options table.

    For anyone else who wants to do this, here’s the main code snippets. Note: “Respite” is my theme name and so I prefix everything with “respite_”. I still need to style/layout a few things better. The full and up-to-date code is still on Github.

    https://github.com/lukejanicke/respite/blob/master/options.php

    function respite_settings_defaults() {
    	$copyright = sprintf( '© %s %s. All rights reserved.', date( 'Y'), get_option( 'blogname' ) );
    	$meta = array (
    		'date' => 1,
    		'category' => 1,
    		'author' => 1,
    		'comments' => 1
    	);
    	$social =  array (
    		'twitter' => '',
    		'facebook' => '',
    		'google' => ''
    	);
    	$options = array (
    		'copyright' => $copyright,
    		'quip' => $quip,
    		'meta' => $meta,
    		'social' => $social
    	);
    	return $options;
    }
    
    function respite_add_default_settings() {
    	$options = respite_settings_defaults();
    	add_option( 'respite', $options );
    }
    add_action( 'after_theme_setup', 'respite_add_default_settings' );
    
    function respite_sanitize_settings( $settings ) {
    	if( isset( $settings['reset'] ) ) {
    		$settings = respite_settings_defaults();
    	}
    	return $settings;
    }
    
    function respite_register_settings() {
    	register_setting( 'respite_settings', 'respite', 'respite_sanitize_settings' );
    }
    add_action( 'admin_init', 'respite_register_settings' );
    
    function respite_options_menu() {
    	add_theme_page('Respite Options', 'Respite Options', 'edit_theme_options', 'respite-settings', 'respite_settings_page');
    }
    add_action( 'admin_menu', 'respite_options_menu' );
    
    function respite_settings_page() {
    ?>
    <div class="wrap">
    	<h2>Respite Options</h2>
    	<form method="post" action="options.php"><?php
    		settings_fields( 'respite_settings' );
    		do_settings_sections( 'respite_settings' );
    		$options = get_option('respite'); ?>
    		<table class="form-table">
    			<tr valign="top">
    				<th scope="row"><label for"copyright">Copyright Notice</label></th>
    				<td><p><input type="text" id="copyright" name="respite[copyright]" value='<?php echo $options['copyright']; ?>' /></p><p><em>Optional.</em> Use this field to enter a copyright notice, or any other notice you would like to appear at the bottom of every page. You can include HTML.</p></td>
    			</tr>
    			<tr valign="top">
    				<th scope="row">Post Meta Fields</th>
    				<td><p><input type="checkbox" id="date" name="respite[meta][date]" value="1" <?php echo checked( 1, $options['meta']['date'], false ); ?> /> <label for"date">Date</label></p>
    					<p><input type="checkbox" id="category" name="respite[meta][category]" value="1" <?php checked( $options['meta']['category'] ); ?> /> <label for"category">Category</label></p>
    <p><input type="checkbox" id="author" name="respite[meta][author]" value="1" <?php checked( $options['meta']['author'] ); ?> /> <label for"author">Author</label></p>
    					<p><input type="checkbox" id="comments" name="respite[meta][comments]" value="1" <?php checked( $options['meta']['comments'] ); ?> /> <label for"comments">Comments</label></p>
    <p>Select the post meta components you would like displayed.</p></td>
    			</tr>
    			<tr valign="top">
    				<th scope="row">Social Network Presences</th>
    				<td>
    					<label for"twitter">Twitter</label>
    					<p><input type="text" id="twitter" name="respite[social][twitter]" value='<?php echo $options['social']['twitter']; ?>' /></p>
    					<label for"facebook">Facebook</label>
    					<p><input type="text" id="facebook" name="respite[social][facebook]" value='<?php echo $options['social']['facebook']; ?>' /></p>
    					<label for"google">Google Plus</label>
    					<p><input type="text" id="google" name="respite[social][google]" value='<?php echo $options['social']['google']; ?>' /></p>
    				</td>
    			</tr>
    		</table>
    		<?php submit_button(); ?>
    		<p class="submit">
    			<input type="submit" class="button button-secondary" name="respite[reset]" value="<?php _e('Reset Settings'); ?>" />
    		</p>
    	</form>
    </div>
    <?php }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘A reset button for a theme’s settings page’ is closed to new replies.