Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

RealSouthpaw on "Custom settings for theme sidebar"

$
0
0

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.wordpress.org/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 all articles
Browse latest Browse all 8245

Trending Articles