Support » Plugins » Hacks » register_setting $option_group param “must exist prior”???

  • The wording in the description of the $option_group param of the register_setting() function confuses me. It seems to indicate that I must “register” or “create” an “option group” before calling register_setting(), but the trail is lost here. How do I “create” or “register” an “option_group” before calling register_setting()?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    If you’re adding settings to an existing settings page, you need to use whatever options group name is used when the form is output. Examine the related PHP source code for the settings_fields( $option_group ) call. Whatever the value of $option_group is is the name to use. Alternately, examine the page’s output HTML source. Within the form tags will be a hidden field named “option_page”. The value of this field is the option group name to use.

    If you’re generating your own page, when you output the form HTML, you must call settings_fields() for the needed hidden fields to be output. Whatever name you use here is the name to supply for $option_group in register_setting(). It’s not so much one must occur before the other, it’s only the arguments in each case have to match. It should also match the menu slug specified when adding the settings menu or sub-menu page.

    This is also the name used when do_settings_sections() is called as part of the form HTML output. Which is also the page name used when calling add_settings_section(). I know it’s confusing. I feel like my explanation is just repeating the same confusing terms. Part of the problem is different labels are assigned to the same value depending on context.

    Here’s the sequence I follow when creating a settings page:
    I make up a meaningful menu slug name for use in adding my menu or sub-menu page. In the menu page callback function that outputs the page’s HTML, where settings_fields() and do_settings_sections() are called, the same menu slug value is used in each case, even though the parameter name in each case is different.

    The same value is also used as the page parameter when calling add_settings_section() and add_settings_field(). The same value is used as the options_group parameter in register_settings(). I said previously that one doesn’t need to occur before the other, which is true regarding settings_fields(). Order does matter within the Settings API. Add the section first, then the related settings fields, before registering each field.

    In summary, regarding menu pages and the Settings API, menu_slug = page = options_group

Viewing 1 replies (of 1 total)
  • The topic ‘register_setting $option_group param “must exist prior”???’ is closed to new replies.