add_settings_field

Home » Functions » add_settings_field

WordPress offers a great deal of flexibility when it comes to customizing themes and plugins. One way to allow users to modify settings is by using the add_settings_field function.

This function adds a new field to an existing settings section within a WordPress admin page. It is used in conjunction with the add_settings_section and register_setting functions to create a complete settings page.

The add_settings_field function includes several parameters including the field ID, title, callback function, page, section ID, and arguments. The field ID is a unique identifier for the field, while the title is the label that appears next to the field on the settings page. The callback function is the code that generates the field’s HTML output.

For example, if you wanted to add a new field to a settings section called "General Settings", you would use the following code:

function my_settings_field_callback() {
    // Generate HTML output for the field
}

function my_settings_page() {
    add_settings_section( 'general_settings', 'General Settings', null, 'my_settings' );
    add_settings_field( 'my_field_id', 'My Field Title', 'my_settings_field_callback', 'my_settings', 'general_settings' );
    register_setting( 'my_settings', 'my_settings_option' );
}

add_action( 'admin_init', 'my_settings_page' );

This code creates a new field with the ID "my_field_id" and the title "My Field Title" within the "General Settings" section on the "my_settings" admin page. The "my_settings_field_callback" function generates the HTML output for the field.

Overall, the add_settings_field function is a powerful tool for developers looking to add custom settings to their WordPress themes and plugins.

Learn More on WordPress.org

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