• So, I’ve been developing this theme I build as my thesis last year and I want to do radiobuttons so user can choose the sidebar location and theme color. The problem is that for some reason my settings doesn’t seem to be saving the sidebar option at all. The selected option doesn’t appear as checked after hitting Save -button. Can somebody look my code and tell me what I’m doing wrong here? All I want right now is just a simple Left or Right question to work.

    <?php
    class NatureLightOptions
    {
    
    private $options;
    
    public function __construct(){
    	add_action( 'admin_menu', array( $this, 'nature_light_register_submenu_page' ) );
            add_action( 'admin_init', array( $this, 'nature_light_register_settings' ) );
    }
    
    public function nature_light_register_submenu_page() {
    	// Add theme options page to the admin menu
            add_theme_page(
                __('Theme Options', "nature-light"),
                __('Theme Options', "nature-light"),
                'manage_options',
                'nature_light_options',
                array( $this, 'nature_light_options_page' )
            );
    }
    
    public function nature_light_register_settings() {
    	// Register settings and call sanitation functions
            register_setting(
                'nature_light_options_group', // Option group
                'nature_light_options', // Option name
                array( $this, 'nature_light_validate' ) // Sanitize
            );
    
            add_settings_section(
                'nature_light_setting_section', // ID
                'Theme Options', // Title
                array( $this, 'print_nature_light_info' ), // Callback
                'nature_light_options_page' // Page
            );  
    
            add_settings_field(
                'theme_color', // ID
                'Theme Color', // Title
                array( $this, 'theme_color_callback' ), // Callback
                'nature_light_options_page', // Page
                'nature_light_setting_section' // Section
            );      
    
            add_settings_field(
                'sidebar_location',
                'Sidebar location',
                array( $this, 'sidebar_location_callback' ),
                'nature_light_options_page',
                'nature_light_setting_section'
            );
    
            add_settings_field(
                'footer_text',
                'Footer text',
                array( $this, 'footer_text_callback' ),
                'nature_light_options_page',
                'nature_light_setting_section'
            );
    } 
    
    public function nature_light_options_page(){
    	// Set class property
            $this->options = get_option( 'nature_light_options' ); ?>
            <div class="wrap">
    
                <h2>Theme Options</h2>
                <form method="post" action="options.php">
                <?php
    		submit_button();
                    // This prints out all hidden setting fields
                    settings_fields( 'nature_light_options_group' );
                    do_settings_sections( 'nature_light_options_page' );
                    submit_button();
                ?>
                </form>
    
            </div><!--.wrap-->
    <?php
    }
    
    public function nature_light_validate($input){
            // Create our array for storing the validated options
    	$new_input = array();
    
            if(isset($input['footer_text']))
                $new_input['footer_text'] = sanitize_text_field( $input['footer_text'] );
    
    	// Loop through each of the incoming options
    	foreach($input as $key => $value){
    
    		// Check to see if the current option has a value. If so, process it.
    		if(isset($input[$key])){
    
    			// Strip all HTML and PHP tags and properly handle quoted strings
    			$new_input[$key] = strip_tags(stripslashes($input[$key]));
    
    		} // end if
    	} // end foreach
    
    	// Return the array processing any additional functions filtered by this action
    	return apply_filters('nature_light_validate', $new_input, $input);
    }
    
    public function print_nature_light_info(){
    	print __('Enter your settings below:', "nature-light");
    }
    
    public function sidebar_location_callback(){
    	$options = get_option('nature_light_options_page');
    
    	printf('<input type="radio" id="sidebarlocation" name="sidebar[location]" value="1"' . checked(1, $options['sidebar_location'], false) . '/> <label>Right</label>
    	<input type="radio" id="sidebarlocation" name="sidebar[location]" value="2"' . checked(2, $options['sidebar_location'], false) . '/> <label>Left</label>' );
    
    }
    
    public function footer_text_callback(){
    	printf(
    	'<input type="text" id="footer_text" name="nature_light_options[footer_text]" value="%s" />',
    	isset( $this->options['footer_text'] ) ? esc_attr( $this->options['footer_text']) : ''
            );
    }
    
    public function theme_color_callback(){
    
    }
    
    } //end of class
    
    if( is_admin() )
        $nature_light_options = new NatureLightOptions();
    ?>
  • The topic ‘Problem with building radiobutton settings’ is closed to new replies.