Function and usage of register_setting() in wordpress


Answers ( 1 )


    The register_setting() function in WordPress is a crucial tool for developers working with settings and options within the WordPress admin area. This function is used to register a setting, enabling WordPress to handle the saving and retrieving of the option's value in a secure and standardized way. Let's break down its usage and parameters:

    Function Signature

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


    • Purpose: Registers a setting and its associated data for a specific option group in WordPress.
    • Usage: Commonly used in plugin or theme development to create custom settings in the WordPress admin area.


    1. $option_group (string, required):

      • The name of the settings group this option belongs to.
      • It should correspond to an allowed option key name.
      • Default allowed option key names include 'general', 'discussion', 'media', 'reading', 'writing', and 'options'.
    2. $option_name (string, required):

      • The name of the option you want to manage.
      • This is the key used to save and retrieve the option's value from the WordPress database.
    3. $args (array, optional):

      • An array of arguments used to specify details about the setting.
      • Possible keys within the array:
        • type (string): The data type of the setting. Valid values include 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
        • description (string): A textual description of the setting.
        • sanitize_callback (callable): A callback function that sanitizes the option’s value before it's saved to the database.
        • show_in_rest (bool|array): Specifies if the setting should be included in the REST API. For complex settings, this can be an array with a 'schema' key.
        • default (mixed): The default value returned when get_option() is called. If not set, it defaults to an empty array.

    Example Usage

    add_action( 'admin_init', function() {
            'general', // Option group
            'my_custom_option', // Option name
                'type'              => 'string',
                'description'       => 'A custom option for my plugin',
                'sanitize_callback' => 'sanitize_text_field',
                'show_in_rest'      => false,
                'default'           => 'default value'

    In this example, a new setting called my_custom_option is registered under the general settings group. It's defined as a string, has a description, uses sanitize_text_field for sanitization, is not shown in the REST API, and has a default value.

    Important Notes

    • Security: Always use a sanitize_callback to ensure the data saved is secure and formatted correctly.
    • Best Practices: Use descriptive and unique names for your options to avoid conflicts with other plugins or WordPress core settings.
    • Retrieving Values: Use get_option( 'option_name' ) to retrieve the value of a registered setting.

Leave an answer