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 How to Build a WordPress Plugin Building a WordPress Plugin Settings Page CRUD with the WordPress Options Table

Form not storing the username variable on refresh?

Hi everyone,

I am currently doing the course on how to build a wordpress plugin, all works ok but the username is not being stored.

Below is my code if you wouldn't mind taking a look and helping to spot the error that would be great.

<?php
$options = array();

function wptreehouse_badges_options_page(){

    if( !current_user_can( 'manage_options' ) ) {

        wp_die( 'You do not have sufficient permissions to access this page.' );

    }

    global $plugin_url;
    global $options;

    if( isset( $_POST['wptreehouse_form_submitted'] ) ) {

        $hidden_field = esc_html( $_POST['wptreehouse_form_submitted'] );

        if( $hidden_field == 'Y' ) {

            $wptreehouse_username = esc_html( $_POST['wptreehouse_username'] );

            $options['wptreehouse_username']  = $wptreehouse_username;
            $options['last_updated']          = time();

            update_option( 'wptreehouse-badges', $options );

        }
    }

    $options = get_option( 'wptreehouse_badges' );

    if( $options != '' ) {

        $wptreehouse_username = $options['wptreehouse_username'];

    }

    require ( 'inc/options-page-wrapper.php' );

}
?>

Here is the html form to :

                            <form name="wptreehouse_username_form" method="post" action="">

                                <input type="hidden" name="wptreehouse_form_submitted" value="Y">

                                <table class="form-table">
                                    <tr>
                                        <td><label for="wptreehouse_username">Treehouse Username</label></td>
                                        <td><input name="wptreehouse_username" id="wptreehouse_username" type="text" value="" class="regular-text" /></td>
                                    </tr>
                                </table>
                                <p>
                                    <input class="button-primary" type="submit" name="wptreehouse_username_submit" value="Save!" />
                                </p>
                            </form>

Hope you can help :)

Spotted the error of my ways,

<?php 

//Wrong Way
update_option( 'wptreehouse-badges', $options );

//Correct Way
update_option( 'wptreehouse_badges', $options );

?>