Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having a devil of a time getting my plugin settings to save. I've gone through the code so many times I have to think I've simply missed something. I'm following the Setting API, but any changes I make on the plugin settings page don't save. What did I miss?

function __construct() {
self::$instance = $this;

//  Add "Settings" link to plugin page
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ) , array( $this, 'settings_link' ) );

// Creates the CEMB Seminar submenu
add_action( 'admin_menu', array( $this, 'add_menu' ) );

// Register and define the settings
add_action( 'admin_init', array( $this, 'settings_init' ) );

} // End of __construct()


/***** Menus *****/

//  Add "Settings" link to plugin page
function settings_link( $links ) {
    $settings_link = sprintf( '<a href="%s">%s</a>', admin_url( 'options-general.php?page=cemb-seminar' ), __( 'Settings' ) );
    array_unshift( $links, $settings_link );
    return $links;
}

// Creates the CEMB Seminar submenu
function add_menu() {
    add_submenu_page( 
        'edit.php?post_type=cemb_seminar', 
        __( 'Course Requirements' ), 
        __( 'Course Requirements' ), 
        'manage_options', 
        'cemb-seminar', 
        array( $this, 'settings_page' )
    );
}

/***** Course Requirement Settings *****/

/* Creates new database fields */
function settings_init() {
    $options = array(
        'cemb_seminar_seminars_college' => '0',
        'cemb_seminar_showcases_college' => '0',
        'cemb_seminar_writers_college' > '0',
        'cemb_seminar_aet_college' => '0',
        'cemb_seminar_mbu_college' => '0',
        'cemb_seminar_eis_college' => '0',
        'cemb_seminar_sng_college' => '0',
    );
    update_option( 'cemb_seminar_options', $options );


    register_setting( 
        'cemb_seminar_options', 
        'cemb_seminar_options' 
    );
    add_settings_section( 
        'cemb_seminar_reqs_college', 
        'Course Requirements', 
        array( $this, 'reqs_college_fn' ), 
        'cemb-seminar'
    );
    add_settings_field( 
        'cemb_seminar_seminars_college_field', 
        'Seminars', 
        array( $this, 'seminars_college_fn' ), 
        'cemb-seminar', 
        'cemb_seminar_reqs_college' 
    );
    add_settings_field( 
        'cemb_seminar_showcases_college_field', 
        'Showcases', 
        array( $this, 'showcases_college_fn' ), 
        'cemb-seminar', 
        'cemb_seminar_reqs_college' 
    );
    add_settings_field( 
        'cemb_seminar_writers_college_field', 
        'Writer\'s Nights', 
        array( $this, 'writers_college_fn' ), 
        'cemb-seminar', 
        'cemb_seminar_reqs_college' 
    );
    add_settings_field( 
        'cemb_seminar_aet_college_field', 
        'AET Seminars', 
        array( $this, 'aet_college_fn' ), 
        'cemb-seminar', 
        'cemb_seminar_reqs_college' 
    );
    add_settings_field( 
        'cemb_seminar_mbu_college_field', 
        'MBU Seminars', 
        array( $this, 'mbu_college_fn' ), 
        'cemb-seminar', 
        'cemb_seminar_reqs_college' 
    );
    add_settings_field( 
        'cemb_seminar_eis_college_field', 
        'EIS Seminars', 
        array( $this, 'eis_college_fn' ), 
        'cemb-seminar', 
        'cemb_seminar_reqs_college' 
    );
    add_settings_field( 
        'cemb_seminar_sng_college_field', 
        'SNG Seminars', 
        array( $this, 'sng_college_fn' ), 
        'cemb-seminar', 
        'cemb_seminar_reqs_college' 
    );

} // End of settings_init()

// Add note about the course requirements settings
function reqs_college_fn() {
    echo '<p>The course requirement options below will apply to all students for all majors.</p>';
}

// Checks for the selected value in the drop menus
function course_req_selected( $selected, $option ) {

    // Get options first
    $options = get_option( 'cemb_seminar_options' );

    // Check if the option matches the input
    if ( $options['$option'] == $selected ) {
        echo ' selected="selected"';
    }   
} // End of course_req_selected()

// Add college-wide Seminars requirements Field
function seminars_college_fn() {

    // Get options from database
    $options = get_option( 'cemb_seminar_options' );
    $option = $options['cemb_seminar_seminars_college'];

    // Build the select form ?>
    <select id="cemb_seminar_seminars_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_seminars_college]">
        <option value="0" <?php if ( $option == '0' ) { echo 'selected="selected"'; } ?>>0</option>
        <option value="1" <?php if ( $option == '1' ) { echo 'selected="selected"'; } ?>>1</option>
        <option value="2" <?php if ( $option == '2' ) { echo 'selected="selected"'; } ?>>2</option>
        <option value="3" <?php if ( $option == '3' ) { echo 'selected="selected"'; } ?>>3</option>
        <option value="4" <?php if ( $option == '4' ) { echo 'selected="selected"'; } ?>>4</option>
        <option value="5" <?php if ( $option == '5' ) { echo 'selected="selected"'; } ?>>5</option>
    </select> <?php

} // End of seminars_college_fn()

// Add college-wide Showcases requirements Field
function showcases_college_fn() {

    // Get options from database
    $options = get_option( 'cemb_seminar_options' );
    $option = $options['cemb_seminar_showcases_college'];

    // Build the select form ?>
    <select id="cemb_seminar_showcases_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_showcases_college]">
        <option value="0" <?php if ( $option == '0' ) { echo 'selected="selected"'; } ?>>0</option>
        <option value="1" <?php if ( $option == '1' ) { echo 'selected="selected"'; } ?>>1</option>
        <option value="2" <?php if ( $option == '2' ) { echo 'selected="selected"'; } ?>>2</option>
        <option value="3" <?php if ( $option == '3' ) { echo 'selected="selected"'; } ?>>3</option>
        <option value="4" <?php if ( $option == '4' ) { echo 'selected="selected"'; } ?>>4</option>
        <option value="5" <?php if ( $option == '5' ) { echo 'selected="selected"'; } ?>>5</option>
    </select> <?php

} // End of showcases_college_fn()

// Add college-wide Writer's Night requirements field
function writers_college_fn() {

    // Get options from database
    $options = get_option( 'cemb_seminar_options' );
    $option = $options['cemb_seminar_writers_college'];

    // Build the select form */?>
    <select id="cemb_seminar_writers_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_writers_college]">
        <option value="0" <?php if ( $option == '0' ) { echo 'selected="selected"'; } ?>>0</option>
        <option value="1" <?php if ( $option == '1' ) { echo 'selected="selected"'; } ?>>1</option>
        <option value="2" <?php if ( $option == '2' ) { echo 'selected="selected"'; } ?>>2</option>
        <option value="3" <?php if ( $option == '3' ) { echo 'selected="selected"'; } ?>>3</option>
        <option value="4" <?php if ( $option == '4' ) { echo 'selected="selected"'; } ?>>4</option>
        <option value="5" <?php if ( $option == '5' ) { echo 'selected="selected"'; } ?>>5</option>
    </select> <?php

} // End of writers_college_fn()

// Add college-wide AET Seminars requirements field
function aet_college_fn() {

    // Get options from database
    $options = get_option( 'cemb_seminar_options' );
    $option = $options['cemb_seminar_aet_college'];

    // Build the select form ?>
    <select id="cemb_seminar_aet_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_aet_college]">
        <option value="0" <?php if ( $option == '0' ) { echo 'selected="selected"'; } ?>>0</option>
        <option value="1" <?php if ( $option == '1' ) { echo 'selected="selected"'; } ?>>1</option>
        <option value="2" <?php if ( $option == '2' ) { echo 'selected="selected"'; } ?>>2</option>
        <option value="3" <?php if ( $option == '3' ) { echo 'selected="selected"'; } ?>>3</option>
        <option value="4" <?php if ( $option == '4' ) { echo 'selected="selected"'; } ?>>4</option>
        <option value="5" <?php if ( $option == '5' ) { echo 'selected="selected"'; } ?>>5</option>
    </select> <?php

} // End of aet_college_fn()

// Add college-wide MBU Seminars requirements field
function mbu_college_fn() {

    // Get options from database
    $options = get_option( 'cemb_seminar_options' );
    $option = $options['cemb_seminar_mbu_college'];

    // Build the select form ?>
    <select id="cemb_seminar_mbu_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_mbu_college]">
        <option value="0" <?php if ( $option == '0' ) { echo 'selected="selected"'; } ?>>0</option>
        <option value="1" <?php if ( $option == '1' ) { echo 'selected="selected"'; } ?>>1</option>
        <option value="2" <?php if ( $option == '2' ) { echo 'selected="selected"'; } ?>>2</option>
        <option value="3" <?php if ( $option == '3' ) { echo 'selected="selected"'; } ?>>3</option>
        <option value="4" <?php if ( $option == '4' ) { echo 'selected="selected"'; } ?>>4</option>
        <option value="5" <?php if ( $option == '5' ) { echo 'selected="selected"'; } ?>>5</option>
    </select> <?php

} // End of mbu_college_fn()

// Add college-wide EIS Seminars requirements field
function eis_college_fn() {

    // Get options from database
    $options = get_option( 'cemb_seminar_options' );
    $option = $options['cemb_seminar_eis_college'];

    // Build the select form ?>
    <select id="cemb_seminar_eis_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_eis_college]">
        <option value="0" <?php if ( $option == '0' ) { echo 'selected="selected"'; } ?>>0</option>
        <option value="1" <?php if ( $option == '1' ) { echo 'selected="selected"'; } ?>>1</option>
        <option value="2" <?php if ( $option == '2' ) { echo 'selected="selected"'; } ?>>2</option>
        <option value="3" <?php if ( $option == '3' ) { echo 'selected="selected"'; } ?>>3</option>
        <option value="4" <?php if ( $option == '4' ) { echo 'selected="selected"'; } ?>>4</option>
        <option value="5" <?php if ( $option == '5' ) { echo 'selected="selected"'; } ?>>5</option>
    </select> <?php

} // End of eis_college_fn()

// Add college-wide SNG Seminars requirements field
function sng_college_fn() {

    // Get options from database
    $options = get_option( 'cemb_seminar_options' );
    $option = $options['cemb_seminar_sng_college'];

    // Build the select form ?>
    <select id="cemb_seminar_sng_college" class="cemb_seminar_settings_field" name="cemb_seminar_options[cemb_seminar_sng_college]">
        <option value="0" <?php if ( $option == '0' ) { echo 'selected="selected"'; } ?>>0</option>
        <option value="1" <?php if ( $option == '1' ) { echo 'selected="selected"'; } ?>>1</option>
        <option value="2" <?php if ( $option == '2' ) { echo 'selected="selected"'; } ?>>2</option>
        <option value="3" <?php if ( $option == '3' ) { echo 'selected="selected"'; } ?>>3</option>
        <option value="4" <?php if ( $option == '4' ) { echo 'selected="selected"'; } ?>>4</option>
        <option value="5" <?php if ( $option == '5' ) { echo 'selected="selected"'; } ?>>5</option>
    </select> <?php

} // End of sng_college_fn()

// Creates the course requirements page
function settings_page() {
    ?>
    <div class="wrap">
    <div class="icon32" style="background-image:url(<?php echo WP_PLUGIN_URL . '/cemb-seminar/images/cemb-logo32x32.png'; ?>); background-repeat:no-repeat;"><br /></div>
    <h2>Course Requirements</h2>
    <form method="post" action="options.php">
    <?php settings_fields( 'cemb_seminar_options' ); ?>
    <?php do_settings_sections( 'cemb-seminar' ); ?>
    <br />
    <input class="button-primary" type="submit" name="Submit" value=" <?php _e( 'Save Settings' ); ?> " />
    </form>
    </div> <?php

} // End of settings_page()
share|improve this question
    
You're filling the option with default values on init and there's no conditional logic to prevent that happening every time you load a page. –  t31os Jul 20 '11 at 9:20

1 Answer 1

up vote 2 down vote accepted

the problem is you keep resting the values here:

 $options = array(
        'cemb_seminar_seminars_college' => '0',
        'cemb_seminar_showcases_college' => '0',
        'cemb_seminar_writers_college' > '0',
        'cemb_seminar_aet_college' => '0',
        'cemb_seminar_mbu_college' => '0',
        'cemb_seminar_eis_college' => '0',
        'cemb_seminar_sng_college' => '0',
    );
    update_option( 'cemb_seminar_options', $options );

change it to

$options = get_option('cemb_seminar_options');
if ($options === false){
$options = array(
            'cemb_seminar_seminars_college' => '0',
            'cemb_seminar_showcases_college' => '0',
            'cemb_seminar_writers_college' > '0',
            'cemb_seminar_aet_college' => '0',
            'cemb_seminar_mbu_college' => '0',
            'cemb_seminar_eis_college' => '0',
            'cemb_seminar_sng_college' => '0',
        );
        update_option( 'cemb_seminar_options', $options );
}

so it will only run if 'cemb_seminar_options' is not set in the database

share|improve this answer
3  
An alternate method would be to use array_merge(). However, whichever route is taken, since this is a Plugin, it should be taking advantage of the activation hook for implementing default settings values. This action only needs to be performed once. –  Chip Bennett Jul 20 '11 at 12:29
    
I agree 100% on the activation hook +1 –  Bainternet Jul 20 '11 at 12:42
    
See? I knew it was something simple I overlooked. Thanks guys, both of your suggestions fixed the issue. –  Slushman Jul 21 '11 at 20:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.