Support » Themes and Templates » Problem with my theme options page

  • Hey everyone, I’m having a bit of an issue with my theme options page and I’m hoping someone can point out my error. I’ve included the code at the bottom, as well as a link to the screenshot of the problem I’m having, and I’ll do my best to explain what I’ve done and what I’m trying to accomplish (I still consider myself a bit of a php n00b so bear with me).

    My first function if_get_defaults is where I’m establishing my option defaults.

    My next function if_options_init (which was referenced from the following guide: http://www.chipbennett.net/2011/02/17/incorporating-the-settings-api-in-wordpress-themes/), from my understanding checks to see if the defaults are written to the database, and if they are not, does so (so essentially setting my default values).

    I then initialize my theme options, register my settings, and add my options page to the admin menu.

    I am using tabs so then I have my function which establishes the tabs.

    Now I actually get to where I establish my options pages themselves, beginning with my general settings. I establish a variable $settings which I use for my convenience when echoing the values into the input fields, and then the settings_fields and do_settings_sections so I can actually save and reset my settings.

    I then lay out each of my settings options, telling it to write the values to their assigned names, and I follow it all off with:

    <p class="submit"><input name="theme_if_options" type="submit" class="button-primary" value="<?php esc_attr_e('Save Defaults', 'theme_if_options'); ?>" /></p>
    	<input name="theme_if_options" type="submit" class="button-secondary" value="<?php esc_attr_e('Reset Defaults', 'theme_if_options'); ?>" />

    I finally have my validate function to strip any HTML from a couple of my values and to avoid parse errors with my check box.

    So, with that all said and done, I am getting a 0 filled into all of my input fields, and I am unable to save any settings. I’m sure there’s just one or two minor things I am overlooking, as I feel like I am on the right track here. Again below you can find the options page in its entirety and a link to what I am seeing in my backend. I know this was a bit lengthy, but any help you could provide to get me on the right track would be greatly appreciated. Thank you in advance.

    <?php
    
    global $if_options;
    
    $file_dir=get_bloginfo('template_directory');
    wp_enqueue_style("functions", $file_dir."/functions/functions.css", false, "1.0", "all");  
    
    // Default Options Values
    function if_get_defaults() {
    	$options = array(
    		'if_site_tagline' => '',
    		'if_home_description' => '',
    		'if_home_keywords' => '',
    		'if_menu_color' => 'Grey',
    		'if_font' => 'Cantarell',
    		'if_menuicon' => '',
    		'if_favicon' => '',
    		'if_fb_like' => '0',
    		'if_ga_code' => '',
    	);
    	return $options;
    
    }
    
    //Set Initial Theme Options
    
    function if_options_init() {
    
    	// set options equal to defaults
    	global $if_options;
    	$if_options = get_option( 'theme_if_options' );
    
    	if ( false === $if_options ) {
    		$if_options = if_get_defaults();
    	}
    	update_option( 'theme_if_options', $if_options );
    }
    
    // Initialize Theme options
    add_action('after_setup_theme', 'if_options_init', 9 );
    
    if ( is_admin() ) : // Load only if we are viewing an admin page
    
    function register_settings() {
    	// Register settings and call sanitation functions
    	register_setting( 'theme_if_options', 'theme_if_options', 'if_options_validate' );
    
    }
    
    add_action( 'admin_init', 'register_settings' );
    
    function theme_options() {
    	// Add theme options page to the admin menu
    	add_theme_page( 'iFeature Pro', 'iFeature Pro', 'edit_theme_options', 'theme_options', 'theme_home_page' );
    }
    
    add_action( 'admin_menu', 'theme_options' );
    
    function mytheme_admin_tabs( $current = 'general' ) {
        $tabs = array( 'general' => 'General', 'header' => 'Header', 'callout' => 'Callout Section', 'slider' => 'iFeature Slider', 'footer' => 'Footer' );
        $links = array();
        foreach( $tabs as $tab => $name ) :
            if ( $tab == $current ) :
                $links[] = "<a class='nav-tab nav-tab-active' href='?page=theme_options&tab=$tab'>$name</a>";
            else :
                $links[] = "<a class='nav-tab' href='?page=theme_options&tab=$tab'>$name</a>";
            endif;
        endforeach;
        echo '<h2>';
        foreach ( $links as $link )
            echo $link;
        echo '</h2>';
    }
    
    // Function to generate options page
    function theme_home_page() {
    	global $pagenow;
    
    	mytheme_admin_tabs();
    
    	if ( $pagenow == 'themes.php' && $_GET['page'] == 'theme_options' ) :
        if ( isset ( $_GET['tab'] ) ) :
            $tab = $_GET['tab'];
        else:
            $tab = 'general';
        endif;
        switch ( $tab ) :
            case 'general' :
                theme_general_options();
                break;
        endswitch;
    	endif;
    }
    
    // Function to generate options page
    function theme_general_options() {
    	global $theme_if_options;
    
    	if ( ! isset( $_REQUEST['updated'] ) )
    		$_REQUEST['updated'] = false; // This checks whether the form has just been submitted. ?>
    
    	<div class="wrap">
    
    	<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options' ) . "</h2>";
    	// This shows the page's name and an icon if one has been provided ?>
    
    	<?php if ( false !== $_REQUEST['updated'] ) : ?>
    	<div class="updated fade"><p><strong><?php _e( 'Options saved' ); ?></strong></p></div>
    	<?php endif; // If the form has just been submitted, this shows the notification ?>
    
    	<form method="post" action="options.php">
    
    	<?php $settings = get_option( 'theme_if_options');
    		  settings_fields('theme_if_options');
    		  do_settings_sections('theme_if_options');
    	?>
    
    	<table class="form-table">
    
    	<tr valign="top"<th scope="row"><label for="if_menu_color">Choose iMenu Color<br />(default: Grey)</label></th>
    	<td>
    	<select id="if_menu_color" name="theme_if_options[if_menu_color]">
    	<?php
    	$menu_color = array( 'Blue', 'Grey', 'Orange', 'Pink', 'Red'); ?>
    	<?
    	foreach ($menu_color as $colors) {
    		$color_choices= $colors ;
    		$selected = '';
    		if ( $color_choices == $settings['if_menu_color'] )
    			$selected = 'selected="selected"';?>
    	<option value="<? echo $color_choices;?>" <? echo $selected; ?>><? echo $color_choices;?></option><?
    	}?>
    
    	</select>
    	</td>
    	</tr>
    
    	<tr valign="top"><th scope="row"><label for="if_site_tagline">Site Tagline<br />(Enter the tagline of your site here.)</label></th>
    	<td>
    	<input id="if_site_tagline" name="theme_if_options[if_site_tagline]" type="text" value="<?php  esc_attr_e($settings['if_site_tagline']); ?>" />
    	</td>
    	</tr>
    
    	<tr valign="top"><th scope="row"><label for="if_home_description">Home Description<br />(Enter the META description of your homepage here.)</label></th>
    	<td>
    	<textarea id="if_home_description" name="theme_if_options[if_home_description]" rows="5" cols="30"><?php echo stripslashes($settings['if_home_description']); ?></textarea>
    	</td>
    	</tr>
    
    	<tr valign="top"><th scope="row"><label for="if_home_keywords">Home Keywords<br />(Enter the META keywords of your homepage here.)</label></th>
    	<td>
    	<textarea id="if_home_keywords" name="theme_if_options[if_home_keywords]" rows="5" cols="30"><?php esc_attr_e($settings['if_home_keywords']); ?></textarea>
    	</td>
    	</tr>
    
    	<tr valign="top"<th scope="row"><label for="if_font">Choose a Font<br />(default: Cantarell)</label></th>
    	<td>
    	<select id="if_font" name="theme_if_options[if_font]">
    	<?php
    	$site_font = array("Cantarell", "Allan", "Allerta", "Amaranth", "Anton", "Arial", "Arimo", "Arvo", "Astloch", "Bentham", "Bevan", "Buda", "Cabin", "Calligraffitti", "Candal", "Cardo", "Chewy", "Coda", "Copse", "Corben", "Cousine", "Crushed", "Cuprum", "Courier New", "Geo", "Georgia", "Gruppo", "Inconsolata", "Kenia", "Kranky", "Kreon", "Lato", "Lekton", "Lobster", "Lucida Grande", "Meddon", "Merriweather", "Michroma", "Molengo", "Neucha", "Neuton", "Nobile", "Nova", "Orbitron", "Oswald", "Pacifico", "Philospher", "Puritan", "Quattrocento", "Radley", "Raleway", "Schoolbell", "Slackey", "Sniglet", "Sunshiney", "Syncopate", "Tahoma", "Tangerine", "Times New Roman", "Tinos", "Ubuntu", "Unkempt", "Verdana", "Vibur", "Vollkorn"); ?>
    	<?
    	foreach ($site_font as $fonts) {
    		$font_choices= $fonts ;
    		$selected = '';
    		if ( $font_choices == $settings['if_font'] )
    			$selected = 'selected="selected"';?>
    	<option value="<? echo $font_choices;?>" <? echo $selected; ?>><? echo $font_choices;?></option><?
    	}?>
    
    	</select>
    	</td>
    	</tr>
    
    	<tr valign="top"><th scope="row"><label for="if_menuicon">Home Menu Icon<br />(Enter the link to your Home menu icon.)</label></th>
    	<td>
    	<input id="if_menuicon" name="theme_if_options[if_menuicon]" type="text" value="<?php  esc_attr_e($settings['if_menuicon']); ?>" />
    	</td>
    	</tr>
    
    	<tr valign="top"><th scope="row"><label for="if_favicon">Custom Favicon<br />(Enter the link to your Favicon here.)</label></th>
    	<td>
    	<input id="if_favicon" name="theme_if_options[if_favicon]" type="text" value="<?php  esc_attr_e($settings['if_favicon']); ?>" />
    	</td>
    	</tr>
    
    	<tr valign="top"><th scope="row"><label for="if_ga_code">Google Analytics<br />(You can paste your Google Analytics or other tracking code in this box. )</label></th>
    	<td>
    	<textarea id="if_ga_code" name="theme_if_options[if_ga_code]" rows="5" cols="30"><?php echo stripslashes($settings['if_ga_code']); ?></textarea>
    	</td>
    	</tr>
    
    	<tr valign="top"><th scope="row"><label for="if_fb_like">Show Facebook Like Button<br />(Check this box to show the Facebook Like Button on blog posts. )</label></th>
    	<td>
    	<input type="checkbox" id="if_fb_like" name="theme_if_options[if_fb_like]" value="1" <?php checked( true, $settings['if_fb_like'] ); ?>></textarea>
    	</td>
    	</tr>
    
    	</table>
    
    	<p class="submit"><input name="theme_if_options" type="submit" class="button-primary" value="<?php esc_attr_e('Save Defaults', 'theme_if_options'); ?>" /></p>
    	<input name="theme_if_options" type="submit" class="button-secondary" value="<?php esc_attr_e('Reset Defaults', 'theme_if_options'); ?>" />
    
    	</form>
    <p>Need help? Follow the links below to visit our support forum and documentation pages:</p>
    <a href="http://cyberchimps.com/forum"><img src="<?php bloginfo('template_url'); ?>/images/forum.png?>" alt="Forum"></a> <a href="http://cyberchimps.com/ifeature-pro/docs"><img src="<?php bloginfo('template_url');?>/images/docs.png?>" alt="Docs"></a>
    
    	</div>
    
    	<?php
    }
    
    function if_options_validate( $input ) {
    	global $if_options;
    
    	$settings = get_option( 'if_theme_options');
    
    	// We strip all tags from the text field, to avoid vulnerablilties like XSS
    	$input['if_site_tagline'] = wp_filter_nohtml_kses( $input['if_site_tagline'] );
    
    	// We strip all tags from the text field, to avoid vulnerablilties like XSS
    	$input['if_home_description'] = wp_filter_post_kses( $input['if_home_description'] );
    
    	// We strip all tags from the text field, to avoid vulnerablilties like XSS
    	$input['if_home_keywords'] = wp_filter_post_kses( $input['if_home_keywords'] );
    
    	// If Show Facebook Like is unchecked, we strip the value to avoid parse errors
    	if ( ! isset( $input['if_fb_like'] ) )
    		$input['if_fb_like'] = null;
    	// We verify if the input is a boolean value
    	$input['if_fb_like'] = ( $input['if_fb_like'] == 1 ? 1 : 0 );
    
    	return $input;
    }
    
    endif;  // EndIf is_admin()

    http://i.imgur.com/bQEnu.png

    If it’s too difficult to follow the code, I’ve uploaded it as a .txt file on one of my test sites here: http://orangeola.com/code/options.txt

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’d be happy to help troubleshoot. Can you post this code as a Pastebin, and link to the Pastebin here in the support topic?

    Thread Starter SeizedPropaganda

    (@seizedpropaganda)

    Thank you chip, I wasn’t sure if you just wanted the link or if you wanted me to embed it, here is the link but I can also embed if that’s what you were asking.

    http://pastebin.com/nmuJnkQ3

    Thread Starter SeizedPropaganda

    (@seizedpropaganda)

    Just thought I’d share something that I discovered which will hopefully help in narrowing this down.

    I was seeing what would cause the 0s to be outputted in my fields and realized that the last part of my if_options_validate function makes the value of my show facebook like box option 0 if unchecked. I temporarily removed that entire function and took it out of the register settings string and now when Save is pressed, the input fields display S, and when Reset is pressed the fields display R. I changed “Save Defaults” to “Jave Defaults” in the value of the save button just to confirm and doing this changes the output to J instead of S, so it’s just taking on the first letter.

    So now it seems that my main function theme_if_options is not operating the way that it should, and is instead taking on the value of the last thing using it. I’m not sure why it’s doing this, but this appears to be the issue so hopefully that will help those that have more experience with this help me narrow it down. Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Problem with my theme options page’ is closed to new replies.