Why aren't these WordPress options being saved to the database correctly?

I have a plugin for editing certain options already stored in the database. It creates an options page under Settings with a simple form that populates each field with whatever values are stored in the database for the given field.

When the page loads, each of the options loaded into the fields are correct, but if I edit any of the fields and click Save changes, all of the fields on the page are cleared out, in addition to deleting their corresponding values from the database. Why is this?

?php
/** Plugin name: Client OAuth/REST Configuration
 * Plugin URI: uri
 * Description: description
 * Author: author
 * Author URI: author uri
 * Version: 0.02
 */  


function register_myplugin_options_settings(){
    register_setting('myplugin_options_group', 'api_username');
    register_setting('myplugin_options_group', 'api_password');
    register_setting('myplugin_options_group', 'client_id');
    register_setting('myplugin_options_group', 'client_secret');
    register_setting('myplugin_options_group', 'auth_url');
    register_setting('myplugin_options_group', 'token_url');
    register_setting('myplugin_options_group', 'rest_url');
    register_setting('myplugin_options_group', 'callback_url');
}
add_action( 'admin_init', 'register_myplugin_options_settings' );


function register_myplugin_options_options_page() {
    add_options_page('Client OAuth REST Configuration', 'Settings', 'manage_options', 'myplugin', 'myplugin_options_page');
}
add_action('admin_menu', 'register_myplugin_options_options_page');


function myplugin_options_page() {
?
    div
    ?php screen_icon(); ?
    h2Oauth/REST Config Setting/h2
        form method=post action=options.php
        ?php settings_fields( 'myplugin_options_group' ); ?
        pTo store the Client Oauth and REST configuration settings in order to access the REST API./p
        table
            tr valign=top
            th scope=rowlabel for=myplugin_api_usernameAPI Login Username/label/th
            tdinput type=text id=myplugin_api_username name=myplugin_api_username style=width: 145px value=?php echo get_option('api_username'); ? //td
        /tr

        tr valign=top
            th scope=rowlabel for=myplugin_api_passwordAPI Login Password/label/th
            tdinput type=password id=myplugin_api_password name=myplugin_api_password style=width: 145px value=?php echo get_option('api_password'); ? //td
        /tr

        tr valign=top
            th scope=rowlabel for=myplugin_client_idOAuth Client ID/label/th
            tdinput type=text id=myplugin_client_id name=myplugin_client_id style=width: 300px value=?php echo get_option('client_id'); ? //td
        /tr

        tr valign=top
            th scope=rowlabel for=myplugin_client_secretOAuth Client Secret/label/th
            tdinput type=password id=myplugin_client_secret name=myplugin_client_secret style=width: 145px value=?php echo get_option('client_secret'); ? //td
        /tr

        tr valign=top
            th scope=rowlabel for=myplugin_auth_urlLogin URL/label/th
            tdinput type=text id=myplugin_auth_url name=myplugin_auth_url style=width: 375px value=?php echo get_option('auth_url'); ? //td
        /tr
  
        tr valign=top
            th scope=rowlabel for=myplugin_token_urlOAuth Token URL/label/th
            tdinput type=text id=myplugin_token_url name=myplugin_token_url style=width: 375px value=?php echo get_option('token_url'); ? //td
        /tr

        tr valign=top
            th scope=rowlabel for=myplugin_rest_urlREST URL/label/th
            tdinput type=text id=myplugin_rest_url name=myplugin_rest_url style=width: 375px value=?php echo get_option('rest_url'); ? //td
        /tr

        tr valign=top
          th scope=rowlabel for=myplugin_callback_urlCallback URL/label/th
          tdinput type=text id=myplugin_callback_url name=myplugin_callback_url style=width: 495px value=?php echo get_option('callback_url'); ? //td
        /tr
        /table
        ?php  submit_button(); ?
    /form
/div
?php
}?

Topic plugin-options settings-api Wordpress

Category Web


The options are not being saved correctly because you used the wrong input name — it should match the second parameter for register_setting(). So for example, with register_setting('myplugin_options_group', 'api_username'), the input name should be api_username, but then you used myplugin_api_username.

Additionally, it should be noted that screen_icon() has long been deprecated.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.