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 trialMS Shohan
Courses Plus Student 163 PointsWhy wptreehouse_username variable doesn't get saved in my case?
Here is the PHP code related to this from the main plugin file
$plugin_url = WP_PLUGIN_URL . '/wptreehouse-badges'; $options = array();
function wptreehouse_badges_options_page() {
if( !current_user_can( 'manage_options') ) {
wp_die( 'You do not have sufficiant 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_username' );
if( $options != '' ) {
$wptreehouse_username = $options[ 'wptreehouse_username' ];
}
require( 'inc/options-page-wrapper.php' );
}
And here is the HTML Form
<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 type="text" value="" class="regular-text" placeholder= "Username*" name="wptreehouse_username" id= "wptreehouse_username"/> </td> </tr> </table> <input class="button-primary" type="submit" name="wptreehouse_username_form" value= "save" /> </form>
Can anyone find my mistake and help me to identify really I what I was doing wrong?
Thank in advance
1 Answer
MS Shohan
Courses Plus Student 163 PointsFigured out the problem on my own.... Actually, the get_option and update_option are not matching and this is the reason my code was not saving.