Creating Theme Options Page

I was creating a theme options page based on a tutorial by Tom McFarlin at TutsPlus and I’m a little stuck. I can’t understand where the code goes bad here, because it doesn’t allow me to access the site at all when its executed.

Is there an easier way to accomplish text input and theme options?

/* =VOTE THEME OPTIONS
---------------------------------------------------- */

/**
 * This function introduces the theme options into the 'Appearance' menu and into a top-level 
 * 'Vote Theme' menu.
 */
function vote_example_theme_menu() {

    add_theme_page(
        'Vote Theme',                   // The title to be displayed in the browser window for this page.
        'Vote Theme',                   // The text to be displayed for this menu item
        'administrator',                    // Which type of users can see this menu item
        'vote_theme_options',           // The unique ID - that is, the slug - for this menu item
        'vote_theme_display'                // The name of the function to call when rendering this menu's page
    );

    add_menu_page(
        'Vote Theme',                   // The value used to populate the browser's title bar when the menu page is active
        'Vote Theme',                   // The text of the menu in the administrator's sidebar
        'administrator',                    // What roles are able to access the menu
        'vote_theme_menu',              // The ID used to bind submenu items to this menu 
        'vote_theme_display'                // The callback function used to render this menu
    );

    add_submenu_page(
        'vote_theme_menu',
        __( 'Header Options', 'vote' ),
        __( 'Header Options', 'vote' ),
        'administrator',
        'vote_theme_header_options',
        create_function( null, 'vote_theme_display( "header_options" );' )
    );


} // end vote_example_theme_menu
add_action( 'admin_menu', 'vote_example_theme_menu' );

/**
 * Renders a simple page to display for the theme menu defined above.
 */
function vote_theme_display( $active_tab = '' ) {
?>
    <!-- Create a header in the default WordPress 'wrap' container -->
    <div class="wrap">

        <div id="icon-themes" class="icon32"></div>
        <h2><?php _e( 'Vote Theme Options', 'vote' ); ?></h2>
        <?php settings_errors(); ?>

        <?php if( isset( $_GET[ 'tab' ] ) ) {
            $active_tab = $_GET[ 'tab' ];
        } else {
            $active_tab = 'header_options';
        } // end if/else ?>

        <h2 class="nav-tab-wrapper">
            <a href="?page=vote_theme_options&tab=header_options" class="nav-tab <?php echo $active_tab == 'header_options' ? 'nav-tab-active' : ''; ?>"><?php _e( 'Header Options', 'vote' ); ?></a>
        </h2>

        <form method="post" action="options.php">
            <?php

                if ( $active_tab == 'header_options' ) {

                    settings_fields( 'vote_theme_header_options' );
                    do_settings_sections( 'vote_theme_header_options' );

                } else {

                    settings_fields( 'vote_theme_header_options' );
                    do_settings_sections( 'vote_theme_header_options' );

                }// end if/else

                submit_button();

            ?>
        </form>

    </div><!-- /.wrap -->
<?php
} // end vote_theme_display

/* ------------------------------------------------------------------------ *
 * Setting Registration
 * ------------------------------------------------------------------------ */ 

/**
 * Provides default values for the Header Options.
 */
function vote_theme_default_header_options() {

    $defaults = array(
        'elect_city'        =>  '',
        'elect_name'        =>  'City Council',
        'elect_year'        =>  '',
        'elect_type'        =>  'Election',
        'elect_date_month'  =>  '',
        'elect_date_day'    =>  '',
    );

    return apply_filters( 'vote_theme_default_header_options', $defaults );

} // end vote_theme_default_header_options

/**
 * Initializes the theme's input example by registering the Sections,
 * Fields, and Settings. This particular group of options is used to demonstration
 * validation and sanitization.
 *
 * This function is registered with the 'admin_init' hook.
 */ 
function vote_theme_initialize_header_options() {

    if( false == get_option( 'vote_theme_header_options' ) ) {  
        add_option( 'vote_theme_header_options', apply_filters( 'vote_theme_default_header_options', vote_theme_default_header_options() ) );
    } // end if

    add_settings_section(
        'header_options_section',
        __( 'Header Options', 'vote' ),
        'vote_header_options_callback',
        'vote_theme_header_options'
    );

    add_settings_field( 
        'Election City',                        
        __( 'Election City', 'vote' ),                          
        'vote_elect_city_callback', 
        'vote_theme_header_options',    
        'header_options_section'            
    );

    add_settings_field( 
        'Election Name',                        
        __( 'Election Name', 'vote' ),                          
        'vote_elect_name_callback', 
        'vote_theme_header_options',    
        'header_options_section'            
    );

    add_settings_field(
        'Election Year',                        
        __( 'Election Year', 'vote' ),                          
        'vote_elect_year_callback', 
        'vote_theme_header_options',
        'header_options_section'    
    );

    add_settings_field(
        'Election Type',                        
        __( 'Election Type', 'vote' ),                          
        'vote_elect_type_callback', 
        'vote_theme_header_options',
        'header_options_section'    
    );

    add_settings_field(
        'Election Date: Month',                     
        __( 'Election Date: Month', 'vote' ),                           
        'vote_elect_date_month_callback',   
        'vote_theme_header_options',
        'header_options_section'    
    );

    add_settings_field(
        'Election Date: Day',                       
        __( 'Election Date: Day', 'vote' ),                         
        'vote_elect_date_day_callback', 
        'vote_theme_header_options',
        'header_options_section'    
    );

    register_setting(
        'vote_theme_header_options',
        'vote_theme_header_options',
        'vote_theme_validate_header_options'
    );

} // end vote_theme_initialize_header_options
add_action( 'admin_init', 'vote_theme_initialize_header_options' );

/* ------------------------------------------------------------------------ *
 * Section Callbacks
 * ------------------------------------------------------------------------ */ 

/**
 * This function provides a simple description for the Input Examples page.
 *
 * It's called from the 'vote_theme_intialize_header_options_options' function by being passed as a parameter
 * in the add_settings_section function.
 */
function vote_header_options_callback() {
    echo '<p>' . __( 'Provide input for the page header section.', 'vote' ) . '</p>';
} // end vote_header_options_callback

/* ------------------------------------------------------------------------ *
 * Field Callbacks
 * ------------------------------------------------------------------------ */ 

/**
 * This function renders the interface elements for toggling the visibility of the header element.
 * 
 * It accepts an array or arguments and expects the first element in the array to be the description
 * to be displayed next to the checkbox.
 */

function vote_header_options_callback() {

    $options = get_option( 'vote_theme_header_options' );

    // Render the output
    echo '<input type="text" id="elect_city" name="vote_theme_header_options[elect_city]" value="' . $options['elect_city'] . '" />';

} // end vote_elect_city_callback

function vote_elect_name_callback() {

    $options = get_option( 'vote_theme_header_options' );

    // Render the output
    echo '<input type="text" id="elect_name" name="vote_theme_header_options[elect_name]" value="' . $options['elect_name'] . '" />';

} // end vote_elect_name_callback

function vote_elect_year_callback($year_limit = 0) {

    $options = get_option( 'vote_theme_header_options' );

    $html = '<select id="elect_year" name="vote_theme_header_options[elect_year]">';
        for ($year = 2014; $year <= (date("Y") - $year_limit); $year++) {
            $html .= '<option value="' . $year . '"' . selected( $options['elect_year'], $year, false) . '>' . __( $year, 'vote' ) . '</option>'."n";
        }

    echo $html;

} // end vote_elect_year_callback

function vote_elect_type_callback() {

    $options = get_option( 'vote_theme_header_options' );

    // Render the output
    echo '<input type="text" id="elect_type" name="vote_theme_header_options[elect_type]" value="' . $options['elect_type'] . '" />';

} // end vote_elect_type_callback

function vote_elect_date_month_callback() {

    $options = get_option( 'vote_theme_header_options' );

    $html = '<select id="elect_date_month" name="vote_theme_header_options[elect_date_month]">';
        $months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
        for ($month = 1; $month <= 12; $month++) {
            $html .= '<option value="' . $month . '"' . selected( $options['elect_date_month'], $month, false) . '>' . __( $months[$month], 'vote' ) . '</option>'."n";
        }

    echo $html;

} // end vote_elect_date_month_callback

function vote_elect_date_day_callback() {

    $options = get_option( 'vote_theme_header_options' );

    $html = '<select id="elect_date_day" name="vote_theme_header_options[elect_date_day]">';
        for ($day = 1; $day <= 31; $day++) {
            $html .= '<option value="' . $day . '"' . selected( $options['elect_date_day'], $day, false) . '>' . __( $day, 'vote' ) . '</option>'."n";
        }

    echo $html;

} // end vote_elect_date_month_callback

/* ------------------------------------------------------------------------ *
 * Setting Callbacks
 * ------------------------------------------------------------------------ */ 

/**
 * Sanitization callback for the social options. Since each of the social options are text inputs,
 * this function loops through the incoming option and strips all tags and slashes from the value
 * before serializing it.
 *  
 * @params  $input  The unsanitized collection of options.
 *
 * @returns         The collection of sanitized values.
 */

function vote_theme_validate_header_options( $input ) {

    // Create our array for storing the validated options
    $output = array();

    // 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
            $output[$key] = strip_tags( stripslashes( $input[ $key ] ) );

        } // end if

    } // end foreach

    // Return the array processing any additional functions filtered by this action
    return apply_filters( 'vote_theme_validate_header_options', $output, $input );

} // end vote_theme_validate_header_options

?>

Related posts

1 comment

  1. vote_header_options_callback() is declared twice ! Trying to declare the same function name twice will cause a fatal PHP error.

    Give another name to your function.

Comments are closed.