Support

Account

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

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