WordPress – How to add reset button in theme options page?

I was working on a wordpress theme project, where i implemented theme options page. Thought that there needs to be a feature to reset all theme values, when a button is clicked. It didn’t work, when i put-up a Reset button, near the submit button on the same form in the wordpress. It turns out i have to include a separate form for this to do the trick. Don’t forget that you can create buttons using wordpress submit_button() function

Here is a snippet of code that does the trick.

<h1>Reset Defaults</h1>
<form method="post" action="">
 <p class="submit">
 Load theme default settings: <input name="reset" class="button button-secondary" type="submit" value="Reset to theme default settings" >
 <input type="hidden" name="action" value="reset" />
 </p>
</form>

Then you write code to do the task when the button is clicked.

if(isset($_POST['reset'])) {
 update_option('mytheme-options', mytheme_defaults() );
 echo '<p style="color:red">Theme settings have been reset and default values loaded</p>'; 
}

The function my theme_defaults() contains an array of default values required for the theme. For example

function my theme_defaults() {
$defaults = array (
 'logo' => '',
 'favicon' => '',
 'footer' => '',
 'facebook-link' => '',
 'twitter-link' => '',
 'google-plus-link' => '',
 'pin-it-link' => '',
 'linkedin-link' => '',
 'showcase-image' => '',
 'showcase-title' => '',
 'showcase-text' => '',
 'showcase-link' => '',
 'menu-color' => '#caeffc',
 'header-color' => 'transparent',
 'menu-font-color' => '#333',
 'widgets-menus-color' => 'transparent',
 'footer-color' => '#b7d3e5',
 'link-color' => '#000',
 'bg-color' => 'transparent',
 'showcase-color' => '#f5fefe',
 'showcase-font-color' => '#777',
 'showcase' => '1',
 'comments' => '1',
 'reset' => '0'
 );

return $defaults; 

}

Similar Posts:

Prabhu Balakrishnan

I am cool and friendly internet entreprenur blogging since 2005. My interests are wordpress, PHP, linux servers, SEO and travel. Besides computers, i am crazy for video games, travel and a cup of hot coffee! I am born in Coimbatore, India but i currently live in Budapest, Hungary. Feel free to contact me folks!

Leave a Reply

Your email address will not be published. Required fields are marked *


+ one = 3

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please wrap all source codes with [code][/code] tags. Powered by