Support » Plugins » Hacks » Trouble saving settings on options page

  • Resolved dbhynds

    (@dbhynds)



    I’m having trouble saving settings. I think I’ve got everything set up correctly, but when I click “Save Changes,” it says “Settings saved” but doesn’t actually update anything in the database. What am I doing wrong. I feel like it’s something obvious.

    By the way, the way I’m doing it is setting up an associative array of setting groups ($setting_groups) with settings in them and looping through it to add and register settings.

    Here’s my code:

    <?php
    var $setting_groups = array(
    	'lazyloadxt-general' => array(
    			'title' => 'General Settings',
    			'settings' => array(
    				'lazyloadxt_min' => array('Minimize Scripts','checkbox'),
    				'lazyloadxt_extra' => array('Extras','checkbox'),
    			)
    		),
    	);
    
    function __construct() {
    	if ( is_admin() ){
    		add_action( 'admin_menu', array($this,'admin_menu') );
    		add_action( 'admin_init', array($this,'register_settings') );
    	}
    }
    function admin_menu() {
    	add_options_page('Lazy Load XT Settings', 'Lazy Load XT', 'administrator','lazyloadxt',array($this,'settings_page'));
    }
    
    function register_settings() {
    	$setting_groups = $this->setting_groups;
    	foreach ($setting_groups as $group => $settings) {
    		add_settings_section(
    	        $group,         // ID used to identify this section and with which to register options
    	        $settings['title'],                  // Title to be displayed on the administration page
    	        array($this,'settings_section_callback'), // Callback used to render the description of the section
    	        'lazyloadxt'                           // Page on which to add this section of options
    	    );
    		foreach ($settings['settings'] as $setting => $setting_args) {
    			register_setting('lazyloadxt',$setting);
    			add_settings_field (
    				$setting,
    				$setting_args[0],
    				array($this,'form_field'),
    				'lazyloadxt',
    				$group,
    				array(
    					'id' => $setting,
    					'type' => $setting_args[1]
    				)
    			);
    		}
    	}
    }
    
    function settings_section_callback($args) {
    }
    
    function form_field($args) {
    	if ($args['type'] == 'checkbox') {
    		$val = (get_option($args['id'])) ? 'checked="checked' : '';
    		echo '<input type="checkbox" value="1" '.$val.' />';
    	}
    }
    
    function settings_page() {
        ?>
        <div class="wrap">
            <?php screen_icon(); ?>
            <h2>Lazy Load XT Settings</h2>
            <form method="post" action="options.php">
            <?php
                // This prints out all hidden setting fields
                settings_fields( 'lazyloadxt' );
                do_settings_sections( 'lazyloadxt' );
                submit_button();
            ?>
            </form>
        </div>
        <?php
    }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Trouble saving settings on options page’ is closed to new replies.