Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Settings API Creating Multiple Setting Fields Saving Multiple Settings in Single Array

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

My mistake, Zac's code was good after all. :/

First of all let me apologise because it seems for the past week or so I've been leading myself and other students who might have been reading up the garden path when it comes to the code for this course.

I followed the course and the apparent "errors" in good faith, thinking that certain code had become obsolete over time.

Turns out I've been getting myself mixed up about the code to add 3 individual fields to a theme options page and then doing the same thing but with all 3 fields in one array. So now I'm feeling a little daft.

That said I still believe the code I posted works, just that there'd needn't have been a problem to fix in the first place if I wasn't trying to use code that fixes one problem to fix another.

So to help myself and others, here's the final complete code concerning how to save multiple settings of an array for one row of data in the WP_options table. Or at least one method of doing so :)

<?php 

function wpt_theme_init() {

    register_setting( 'wptsettings-group', 'wpt_settings' );
    register_setting( 'wptsettings-group', 'wpt_input_test' );
    register_setting( 'wpt_settings-group', 'wpt_select_test');

    add_settings_section( 
        'wpt_slideshow_section',
        'Slideshow Settings',
        'wpt_slideshow_section_callback',
        'wptsettings'
    );
    add_settings_field(
        'wpt_slideshow_checkbox',
        'Show slideshow on homepage',
        'wpt_slideshow_checkbox_callback',
        'wptsettings',
        'wpt_slideshow_section'
    );
    add_settings_field(
        'wpt_slideshow_input',
        'Text input field',
        'wpt_slideshow_input_callback',
        'wptsettings',
        'wpt_slideshow_section'
    );
    add_settings_field(
        'wpt_slideshow_select',
        'Dropdown field',
        'wpt_slideshow_select_callback',
        'wptsettings',
        'wpt_slideshow_section'
    );/**/      

}

add_action( 'admin_init', 'wpt_theme_init');

function wpt_slideshow_section_callback() {

}

function wpt_slideshow_checkbox_callback() {

    $options = get_option( 'wpt_settings' );    
    if( !isset( $options['show_slideshow'] ) ) $options['show_slideshow'] = 0;

    $html = '<input type="checkbox" id="wpt_show_slideshow" name="wpt_settings[show_slideshow]" value="1"' . checked( 1, $options['show_slideshow'], false ) . '/>';
    $html .= '<label for="wpt_show_slideshow">Check to enable slideshow on the homepage</label>';

    echo $html;

}

function wpt_slideshow_input_callback() {

    $options = get_option( 'wpt_settings' );    
    if( !isset( $options['input_test'] ) ) $options['input_test'] = '';

    echo '<input type="text" id="wpt_input_test" name="wpt_settings[input_test]" value="' . $options['input_test'] . '" placeholder="Example field theme setting">';
    //echo '<input type="text" id="wpt_input_test" name="wpt_input_test" value="' . $options . '" placeholder="Example field theme setting">';
}

function wpt_slideshow_select_callback() {

    $options = get_option( 'wpt_settings' );
    //if( isset( $options['wpt_select_test'] ) ) $options['wpt_select_test'] = 1;

?>

    <select name="wpt_settings[select_test]">
        <option value="1" <?php selected( $options['select_test'], 1 ); ?>>1</option>
        <option value="2" <?php selected( $options['select_test'], 2 ); ?>>2</option>
        <option value="3" <?php selected( $options['select_test'], 3 ); ?>>3</option>
    </select>