Support

Account

Home Forums Feature Requests Button to Reset to Default Values Reply To: Button to Reset to Default Values

  • 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;
    }