Support

Account

Home Forums Feature Requests Button to Reset to Default Values

Solving

Button to Reset to Default Values

  • It’d be nice, particularly on Options pages, if we could have a Reset button somewhere that would wipe out any saved values and replace them with the default values we specified (if any).

  • Hi @AmandaB

    Thanks for the request. Your right, this would be a great feature and I’ll add it to the to-do

    Cheers
    Elliot

  • Thanks!

    Btw, I work with ACF nearly every day & couldn’t imagine setting up a site without it. It’s very easy to use and extend, and I love how active & supportive you are with it 🙂

  • Hi @elliot

    I found this topic of 2013…

    This option is already available? It really does lack.

    I am customer ACF PRO.

    Thank you!

  • This isn’t built into ACF yet, but it can be done with a little work. I recently did this for a set of fields I was working on.

    In my case, resetting default values simply means deleting any value that’s already set and this causes the site to revert to using the default values, it also on an options page.

    The first thing I did was to created a message field and set New Lines to No Formatting with a message like this:

    
    To reset all of the values under this tab to their default values click this button. All current settings will be lost and this action cannot be undone.<br /><a class="button reset-options" href="javascript: my_options_reset_defaults();">Reset</a>
    

    all of the function names in all the code I’m posting have been changed.

    The next step was to create my javascript function, it’s an AJAX action.

    
    function my_options_reset_defaults() {
      if (!confirm('Are you sure you want to reset the default options?\nThis operation cannot be undone.')) {
        return;
      }
      jQuery.post(
        my_options_object._ajax_url, {
          'action': 'my_options_reset',
          'nonce': my_options_object._nonce
        },
        function (data) {
          // does not matter what's returned
          // reloads the options page to show defaults
          location.reload();
        }
      );
    }
    

    The above javascript needs to be enqueued on my options page

    
    add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');
    function my_admin_enqueue_scripts() {
      $screen = get_current_screen();
      if ($screen->id == 'settings_page_my-options_page') {
        $handle = 'my-script-handle';
        $src = apply_filters('impexium-integrations/plugin-url', '').'js/my-script.js';
        $deps = array('acf-input', 'acf-pro-input');
        $version = '';
        $in_footer = false;
        wp_register_script($handle, $src, $deps, $version, $in_footer);
        
        $object_name = 'my_options_object';
        $data = array(
          '_ajax_url' => admin_url('admin-ajax.php'),
          '_nonce' => wp_create_nonce('my_options_reset_nonce')
        );
        wp_localize_script($handle, $object_name, $data);
        wp_enqueue_script($handle);
      }
    }
    

    The last step was to create my ajax action that resets the options values

    
    add_action('wp_ajax_my_options_reset', 'reset_default_options');
    function reset_default_options() {
      if (!wp_verify_nonce($_POST['nonce'], 'my_options_reset_nonce')) {
        echo false;
        exit;
      }
      // reset values in a repeater
      $repeater = 'repeater_name';
      // list of sub field in repeater
      $subfields = array(
        'sub_field_1',
        'sub_field_1'
      );
      // list of other fields to be reset
      $fields = array(
        'field_name_1',
        'field_name_2',
        'field_name_3',
        'etc'
      );
      $repeater_count = intval(get_option('options_'.$repeater, 0));
      for ($i=0; $i<$repeater_count; $i++) {
        foreach ($subfields as $subfield) {
          delete_option('options_'.$repeater.'_'.$i.'_'.$subfield);
          delete_option('_options_'.$repeater.'_'.$i.'_'.$subfield);
        }
      }
      delete_option('options_'.$field);
      delete_option('_options_'.$field);
      foreach ($fields as $field) {
        delete_option('options_'.$repeater);
        delete_option('_options_'.$repeater);
      }
      echo true;
      exit;
    }
    
  • Hello @hube2 @elliot ,

    Sorry, I know very little programming and could not make it work.

    I managed to create the “message” field and popup “Are you sure?” It is displayed, but to confirm the page is not loaded and the options do not reset.

    Could you give details of where to enter each code, for me to follow your procedure?

    Thank you!

  • So if you’re getting the confirmation popup then you’ve got the field set up with the button and the script is being loaded.

    Please post the code for each of these things and I will attempt to look at it and see if there is anything I can see that would be stopping it from working.

    1) The function that enqueues the JavaScript
    2) The JavaScript function
    3) The function that performs the AJAX request along with the add_action() call for it.

  • Thank you for your support!

    See the attached image also to see the ACF plugin settings.

    Sorry, like I said, I lay in programming …

    I try to do step by step.

    The code below is in the file “functions.php”

    <?php
    /**
     * Twenty Fifteen functions and definitions
     *
     * Set up the theme and provides some helper functions, which are used in the
     * theme as custom template tags. Others are attached to action and filter
     * hooks in WordPress to change core functionality.
     *
     * When using a child theme you can override certain functions (those wrapped
     * in a function_exists() call) by defining them first in your child theme's
     * functions.php file. The child theme's functions.php file is included before
     * the parent theme's file, so the child theme functions would be used.
     *
     * @link https://codex.wordpress.org/Theme_Development
     * @link https://codex.wordpress.org/Child_Themes
     *
     * Functions that are not pluggable (not wrapped in function_exists()) are
     * instead attached to a filter or action hook.
     *
     * For more information on hooks, actions, and filters,
     * {@link https://codex.wordpress.org/Plugin_API}
     *
     * @package WordPress
     * @subpackage Twenty_Fifteen
     * @since Twenty Fifteen 1.0
     */
    
    if( function_exists('acf_add_options_page') ) {
    	
    	//acf_add_options_page();
    	
    	acf_add_options_page(array(
    		'page_title' 	=> 'Configura&ccedil;&otilde;es Gerais do Tema',
    		'menu_title'	=> 'Tema Teste',
    		'menu_slug' 	=> 'tema-teste',
    		'capability'	=> 'edit_posts',
    		'redirect'		=> true
    	));
    	
    }
    
    // add Fancybox to admin
    function custom_admin_js() {
        $fancybox_js = get_bloginfo('template_directory') . '/js/jquery-3.1.0.min.js';
        echo '<script type="text/javascript" src="'. $fancybox_js . '"></script>';
    }
    add_action('admin_footer', 'custom_admin_js');
    // add function to admin
    function add_jquery_icon_menus() {
    ?>
    	
    	<script>
    		function my_options_reset_defaults() {
    			if (!confirm('Tem certeza?\nNao pode ser desfeita.')) {
    				return;
    			}
    			jQuery.post(
    				my_options_object._ajax_url, {
    					'action': 'my_options_reset',
    					'nonce': my_options_object._nonce
    				},
    				function (data) {
    					// does not matter what's returned
    					// reloads the options page to show defaults
    					location.reload();
    				}
    			);
    		}
    	</script>
    	
    <?php
    }
    add_action('admin_footer', 'add_jquery_icon_menus');
    
    ///////////////////
    add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');
    function my_admin_enqueue_scripts() {
      $screen = get_current_screen();
      if ($screen->id == 'settings_page_my-options_page') {
        $handle = 'my-script-handle';
        $src = apply_filters('impexium-integrations/plugin-url', '').'js/my-script.js';
        $deps = array('acf-input', 'acf-pro-input');
        $version = '';
        $in_footer = false;
        wp_register_script($handle, $src, $deps, $version, $in_footer);
        
        $object_name = 'my_options_object';
        $data = array(
          '_ajax_url' => admin_url('admin-ajax.php'),
          '_nonce' => wp_create_nonce('my_options_reset_nonce')
        );
        wp_localize_script($handle, $object_name, $data);
        wp_enqueue_script($handle);
      }
    }
    
    ///////////////////////////
    
    add_action('wp_ajax_my_options_reset', 'reset_default_options');
    function reset_default_options() {
      if (!wp_verify_nonce($_POST['nonce'], 'my_options_reset_nonce')) {
        echo false;
        exit;
      }
      // reset values in a repeater
      $repeater = 'repeater_name';
      // list of sub field in repeater
      $subfields = array(
        'campo_teste',
      );
      // list of other fields to be reset
      $fields = array(
        'campo_texto',
      );
      $repeater_count = intval(get_option('options_'.$repeater, 0));
      for ($i=0; $i<$repeater_count; $i++) {
        foreach ($subfields as $subfield) {
          delete_option('options_'.$repeater.'_'.$i.'_'.$subfield);
          delete_option('_options_'.$repeater.'_'.$i.'_'.$subfield);
        }
      }
      delete_option('options_'.$field);
      delete_option('_options_'.$field);
      foreach ($fields as $field) {
        delete_option('options_'.$repeater);
        delete_option('_options_'.$repeater);
      }
      echo true;
      exit;
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  • I’m going to look at this one piece at a time. The first part is where the function for enqueuing the script. My function was just meant as an example and needed you do supply your own values

    
    ///////////////////
    add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');
    function my_admin_enqueue_scripts() {
      $screen = get_current_screen();
      if ($screen->id == 'settings_page_my-options_page') {
        $handle = 'my-script-handle';
        // the $src value needs to be set to point to
        // your script. Since you are doing this in the
        // theme function.php file is should look something
        // like the following. Note that you still need to
        // update the path and file name to match your own
        $src = get stylesheet directory uri().'/path/to/script/script-name.js';
        $deps = array('acf-input', 'acf-pro-input');
        wp_register_script($handle, $src, $deps);
        
        // your should change the object name to
        // match what your doing
        $object_name = 'my_options_object';
        $data = array(
          '_ajax_url' => admin_url('admin-ajax.php'),
          // your should use a unique nonce to create the nonce
          '_nonce' => wp_create_nonce('my_options_reset_nonce')
        );
        wp_localize_script($handle, $object_name, $data);
        wp_enqueue_script($handle);
      }
    }
    
  • The AJAX function

    
    add_action('wp_ajax_my_options_reset', 'reset_default_options');
    function reset_default_options() {
      // the nonce name here needs to be the same as the
      // the nonce name you created when enqueuing the script
      if (!wp_verify_nonce($_POST['nonce'], 'my_options_reset_nonce')) {
        echo false;
        exit;
      }
      // reset values in a repeater
      // this needs to be the name of the repeater
      // you want to clear
      $repeater = 'repeater_name';
      // list of sub field in repeater
      $subfields = array(
        'campo_teste',
      );
      // list of other fields to be reset
      // this is a list of other fields that
      // are not part of a repeater
      $fields = array(
        'campo_texto',
      );
      $repeater_count = intval(get_option('options_'.$repeater, 0));
      for ($i=0; $i<$repeater_count; $i++) {
        foreach ($subfields as $subfield) {
          delete_option('options_'.$repeater.'_'.$i.'_'.$subfield);
          delete_option('_options_'.$repeater.'_'.$i.'_'.$subfield);
        }
      }
      delete_option('options_'.$repeater);
      delete_option('_options_'.$repeater);
      foreach ($fields as $field) {
        delete_option('options_'.$repeater);
        delete_option('_options_'.$repeater);
      }
      echo true;
      exit;
    }
    
  • This function needs to be put into the JavaScript file that you’re enqueuing

    
    function my_options_reset_defaults() {
      if (!confirm('Are you sure you want to reset the default options?\nThis operation cannot be undone.')) {
        return;
      }
      jQuery.post(
        // the object name needs to be the same
        // as the object name used when
        // localizing the script
        my_options_object._ajax_url, {
          // the action needs to be the same
          // as the wp_ajax_ action you set up
          'action': 'my_options_reset',
          'nonce': my_options_object._nonce
        },
        function (data) {
          // does not matter what's returned
          // reloads the options page to show defaults
          location.reload();
        }
      );
    }
    
  • Hello @hube2

    Finally I got to work, for your help!

    Thank you very much for real!!! 😀

  • Hey everyone, I am finding this post in July of 2019. Just wondering if this still works or not? I have been trying to kick this off in custom theme I am writing for a client and I have tried about 8 different ways so far and am not able to make this work. I really need this reset, lol. Any thoughts on current compatability?

  • Recently I needed something similar and I couldn’t find anything so I made a simple plugin.

    For anyone still looking for a reset button:
    https://wordpress.org/plugins/reset-button-for-acf/

Viewing 14 posts - 1 through 14 (of 14 total)

The topic ‘Button to Reset to Default Values’ is closed to new replies.