register_setting

Home » Functions » register_setting

If you’re a WordPress developer, you’re probably familiar with the register_setting function. It’s a crucial function for creating custom settings pages in WordPress. In a nutshell, register_setting allows you to create a new setting and register it with WordPress, so that it can be displayed and saved on the settings page.

To use register_setting, you just need to provide a few parameters. The first parameter is the name of the settings group that your setting belongs to. This is important because it determines where your setting will appear on the settings page. The second parameter is the name of the setting itself, which is used as the key for storing the value in the database. Finally, you need to provide a callback function that is responsible for validating and sanitizing the input.

Here’s an example of how to use register_setting to create a new setting for your theme:

// Register the new setting
function my_theme_register_settings() {
    register_setting( 'my_theme_settings_group', 'my_theme_setting', 'my_theme_sanitize_callback' );
}
add_action( 'admin_init', 'my_theme_register_settings' );

// Sanitize and validate the input
function my_theme_sanitize_callback( $input ) {
    // Do some validation and sanitization here
    return $input;
}

In this example, we’re creating a new setting called "my_theme_setting" that belongs to the "my_theme_settings_group" group. We’re also providing a callback function called "my_theme_sanitize_callback" that will be used to sanitize and validate the input. Once your setting is registered, it will appear on the settings page in the appropriate section.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.