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

Mr. Balakrishnan Prabhu is an entrepreneur and world's leading expert in Global citizenship and investor programs assisting high net worth immigrant investor clients. He is also a software engineer with background in Computer science engineering. His other interests are Linux servers, Machine learning, Parallel computing, Computer vision, Wordpress, etc. He is originally born in India currently living in Budapest, Hungary.

  • fox

    Thanks a lot 😀