0

I'm stumped. I've been researching this like crazy, but I can't seem to figure out why register_setting() isn't working for me. I'm new to WP plugin development (obviously), so it's probably just something silly that I'm missing. Check it out.

I'm running this in the constructor function:

add_action( 'admin_init', array( $this, 'register_settings_and_fields') );

Then I have the following code which should execute what I need:

/**
 * Register the settings and fields for the plugin settings page.
 *
 * @since    1.0.0
 */
public function register_settings_and_fields() {

    register_setting('my_plugin_settings', 'my_plugin_settings');

    add_settings_section(
        'my_plugin_main_section',                          // ID used to identify this section and with which to register options
        'Main Settings',                                   // Title to be displayed on the administration page
        array($this, 'my_plugin_main_settings_callback'),  // Callback used to render the description of the section
        $this->plugin_slug                                 // Page on which to add this section of options
    );

    // Begin defining the fields
    add_settings_field('my_plugin_data_1', 'Data 1', array( $this, 'my_plugin_data_1_setting' ), $this->plugin_slug, 'my_plugin_main_section');
}

// ... and of course other lines of code.

Of course, I have more code where I do things such as define the callback functions, etc. Everything seems to execute perfectly, and the settings section/field is displayed correctly. But when I go to /wp-admin/options.php, my_plugin_settings has not been created.

I'm stumped. Any thoughts? Let me know if you'd like to see more code.

1

I am not exactly sure what you mean by saying that you can see your settings section and fields displayed correctly, but your my_plugin_settings has not been created.

I know sometimes I forget to add the actual admin part in which generally looks something like this:

settings_fields( 'my_plugin_settings' );
do_settings_sections( 'my_plugin_settings' );

submit_button();

Or is that you are having trouble when saving the form? If that's the case then I would need to see your callback function.

Sorry if this is not accurate to what you need, but I can;t currently comment on your question to clarify more before posting an answer. If this does not answer then let me know and I will adjust my answer accordingly.

EDIT #1:

I will be honest and say I am not 100% sure that the settings will automatically be added to the database. What I do right before adding the settings section and settings field is this.

/* If the options do not exist then create them*/
if ( false == get_option( 'my_plugin_settings' ) ) {
    add_option( 'my_plugin_settings' );
}

This will create the option in your database.

  • Hi Nick, thanks for the response. I do have the the admin part that you listed already coded in. What I meant is that the actual html fields are added to the settings page correctly, but no my_plugin_settings field is added to the database. It's my understanding that JUST the register_setting() function alone should be enough to add the my_plugin_setting field to the database (although it wouldn't contain any data yet). Is that correct? I mean, after just running register_setting() I should see my variable listed on the wp-admin/options.php page, right? – Pete Apr 26 '14 at 5:12
  • 1
    Hey Pete just updated my answer but thought I would ask also, have you tried saving any data and then checking the database to see if they were created? – Nick Young Apr 26 '14 at 5:25
  • 1
    No, the settings aren't automatically added to the database by register_setting(). The setting won't be added to the database until the form is submitted. – J.D. Apr 26 '14 at 14:05
  • Awesome! Got it. Just a matter of my poor understanding of how that worked. Thanks! – Pete Apr 26 '14 at 15:43
0

in my case what solved the problem after debugging options.php regarding the $whitelist_options variable, was to make the $option_group argument the same as the options page slug.

In your example

register_setting($this->plugin_slug, 'my_plugin_settings');

add_settings_section(
        'my_plugin_main_section',                          // ID used to identify this section and with which to register options
        'Main Settings',                                   // Title to be displayed on the administration page
        array($this, 'my_plugin_main_settings_callback'),  // Callback used to render the description of the section
        $this->plugin_slug                                 // Page on which to add this section of options
    );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.