1

When registering options in WordPress, you are required to supply a "settings group name" along with the name of the option itself.

register_setting( string $option_group, string $option_name, array $args = array() )

Given,

register_setting($my_settings_group, $my_option_name1);
register_setting($my_settings_group, $my_option_name2);
register_setting($my_settings_group, $my_option_name3);

Is it possible to then get a list of all options for the specified custom settings group?

For example, something appropriate to the pseudocode...

get_settings_group_options($my_settings_group);

which would return either an array of option names, or option names and corresponding option values.

array(
    'my_option_name1 => 'option_value1', 
    'my_option_name2 => 'option_value2', 
    'my_option_name3 => 'option_value3',
)

If not, then whats point of specifying the settings group name in the first place?

1
  • it may be useful to know that this API is primarily for settings pages, not for organising options themselves, you would rarely want to do this unless you were manually constructing your own settings page using the settings API used for pages under settings by WP, which is rare as the settings API is intended to do that for you
    – Tom J Nowell
    Commented May 26, 2022 at 17:01

1 Answer 1

1

Because the group is not the same as a category or taxonomy. It's used to determine where that settings UI control is displayed in the WP Admin area.

e.g. the value general is for the general settings admin screen, not because it's a general options group.

Until the introduction of the settings endpoint and the REST API, the primary purpose of this function was to let WordPress know about an option so that it would know to save that option, and as a part of the settings GUI API.

Most commonly it would be combined with a call to add_settings_field, this would tell WP that to add a field for a setting with a callback to display it, and which page it will appear on.

Then, on that page, would be a do_settings_sections call that's given the page, e.g. 'general', and calls to do_settings_fields that would take a page and a section.

e.g. adding a setting to the general settings page:

// Registering the field
function wpdocs_add_option_field_to_general_admin_page() {
 
    register_setting( 'general', 'Field_Name_To_Add' );
 
    add_settings_field( 
        'field_id-to-add', 
        'Field Name To Display', 
        'wpdocs_setting_callback_function', //Function to Call
        'general', 
        'default', 
        array( 
            'id' => 'field_id-to-add', 
            'option_name' => 'Field_Name_To_Add'
        )
    );
}
 
// Adding options to registered field
add_action( 'admin_menu', 'wpdocs_add_option_field_to_general_admin_page' ); // CallBack Function
 
function wpdocs_setting_callback_function( $val ) {
    $id = $val['id'];
    $option_name = $val['option_name'];
    ?>
    <input 
        type="tel" //this can be any HTML input type: date, number, text etc.
        name="<?php echo esc_attr( $option_name ) ?>"
        id="<?php echo esc_attr( $id ) ?>"
        value="<?php echo esc_attr( get_option( $option_name ) ) ?>" // Displays the value stored in DB
    /> 
    <?php
}

Note here that there is no "group" of options called general, and it only exists in certain circumstances ( you wouldn't add a settings control on the frontend, only in WP Admin ).

So this isn't something you can use to provide structure for options in the database.

More recently register_setting has been used to define default values for options, and as a way to expose options in the REST API.


The closest to what you were hoping for though, is get_registered_settings:

https://developer.wordpress.org/reference/functions/get_registered_settings/

This will give you the main registered settings array. You would then need to loop through each setting, filtering out the ones that don't match your desired group.

Note that this is not a way to get a list of all stored options, as not all options are registered, and it isn't commonplace in most plugins and themes yet. It also won't show transients etc for the same reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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