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" />

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 an entrepreneur and graduate in Computer science engineering, pretty cool chap, blogging since 2005. My interests are Machine learning, Parallel computing, Computer vision, PHP, Wordpress, Linux etc. I like to chill out with travels, video games and a cup of hot coffee. I am born in India, currently living in Budapest, Hungary. Feel free to contact me anytime!

Leave a Reply

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