• Resolved RealSouthpaw

    (@realsouthpaw)


    I wanted to try doing my first own theme and I’m doing custom settings for it right now. I want user to have a chance to decide if s/he wants sidebar be located on left or right side of the screen and in order to do that setting I studied these tutorials:

    http://wp.tutsplus.com/tutorials/theme-development/create-a-settings-page-for-your-wordpress-theme/
    http://codex.ww.wp.xz.cn/Creating_Options_Pages

    With these I managed to do settings page and form with radiobuttons but since all tutorials I’ve found are using text form as an example I can’t now figure out how to save the radiobutton setting and how to use it in actual theme. I would be really grateful if somebody would explain to me what to do. Here is the code I have for settings page this far:

    <?php //Settings page for theme is done here
    function setup_theme_admin_menus() {
    	add_menu_page('Theme Options', 'Theme Options', 'manage_options', 'theme_settings', 'theme_options', '', '58');
    
    	add_submenu_page('tut_theme_settings', 'Sidebar', 'Sidebar', 'manage_options', 'theme_settings', 'theme_options');
    
    	//Call register settings function
    	add_action( 'admin_init', 'register_mysettings');
    }
    
    // This tells WordPress to call the function named "setup_theme_admin_menus"
    // when it's time to create the menu pages.
    add_action("admin_menu", "setup_theme_admin_menus");  
    
    //Registers options needed for this theme
    function register_mysettings() {
      register_setting( 'themeoption-group', 'sidebar_location' );
    }
    
    //This is the actual settings page for theme
    function theme_options() {
    	// Check if current user is allowed to update options
    	if (!current_user_can('manage_options')) {
    		wp_die('You do not have sufficient permissions to access this page.');
    	}
    	$sidebar_location = get_option("sidebar_location"); ?>
    
    	<div class="wrap">
    		<h2>Theme Options</h2>
    		<hr>
    		<form method="post" action="options.php">
    			<?php
    			settings_fields( 'themeoption-group' );
    			do_settings_sections( 'themeoption-group' ); ?>
    
    			<label>Sidebar location:</label><br/>
    			<input type="radio" name="sidebarlocation" value="right" <?php if($sidebar_location == "right"){echo "checked";} else{ }?>>Right<br>
    			<input type="radio" name="sidebarlocation" value="left" <?php if($sidebar_location == "left"){echo "checked";} else{ }?>>Left
        			<input type="hidden" name="update_settings" value="Y" />
    			<?php submit_button(); ?>
    		</form>
    	</div>
    
    <?php 
    
    	if (isset($_POST["update_settings"])) {
        		$sidebar_location = esc_attr($_POST["sidebarlocation"]);
    		update_option("sidebar_location", $sidebar_location);
    	} ?>
Viewing 1 replies (of 1 total)
  • Thread Starter RealSouthpaw

    (@realsouthpaw)

    Resolved. Classmate figured out how to do it so settings are now working perfectly. In case that, someone need something similar in future, here is the code we did:

    //Settings page for theme is done here
    function setup_theme_admin_menus() {
    	add_menu_page('Theme Options', 'Theme Options', 'manage_options', 'theme_settings', 'theme_options', '', '58');
    	add_submenu_page('tut_theme_settings', 'Sidebar', 'Sidebar', 'manage_options', 'theme_settings', 'theme_options');
    }
    
    // This tells WordPress to call the function named "setup_theme_admin_menus"
    // when it's time to create the menu pages.
    add_action("admin_menu", "setup_theme_admin_menus");  
    
    //Registers options needed for this theme
    function register_mysettings() {
     	register_setting( 'themeoption-group', 'chooseright', 'right');
    }
    add_action( 'admin_init', 'register_mysettings' );
    
    //This is the actual settings page for theme
    function theme_options() {
    	//Checks if hidden field in form is submitted
    	if (isset($_POST["update_settings"])){  
    
    		//Selects the previous value of the field, to restore it in case an invalid entry has been given
        		$prev = $sidebar_location;
    		$sidebar_location = esc_attr($_POST["sidebarlocation"]);
    
        		//Verifies if the given value exists in the layouts array
        		if ($sidebar_location == 'right' || $sidebar_location == 'left'){
        			update_option("sidebar_location", $sidebar_location);
    		}
    		else{ update_option("sidebar_location", $prev); } ?>
    
    	<div id="message" class="updated">
            	<p><strong><?php _e('Settings saved.') ?></strong></p>
        	</div>
    	<?php }?>
    
    	<? $sidebar_location = get_option("sidebar_location");
    	echo $sidebar_location; ?>
    
     	<div class="wrap">
     		<h2>Theme Options</h2>
    		<hr>
    		<form method="post" action="">
    			<label>Sidebar location:</label><br/>
    			<input type="radio" name="sidebarlocation" value="right" <?php if($sidebar_location == "right"){echo "checked";} else{ }?>>Right<br>
    			<input type="radio" name="sidebarlocation" value="left" <?php if($sidebar_location == "left"){echo "checked";} else{ }?>>Left
        			<input type="hidden" name="update_settings" value="Y" />
    			<?php submit_button(); ?>
    		</form>
    	</div>
    <?php
    }
    ?>
Viewing 1 replies (of 1 total)

The topic ‘Custom settings for theme sidebar’ is closed to new replies.