Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 );

?>