How to Use the Settings API to Add Settings to an Existing Page in WordPress

Home » Snippets » How to Use the Settings API to Add Settings to an Existing Page in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

When building a WordPress theme or plugin, there may be times when you want to add additional settings to an existing page. This can be done easily with the Settings API, which allows you to create a settings page and add fields and options to it. In this article, we’ll show you how to use the Settings API to add custom settings to an existing page, without having to create a new page.

					function WPTurbo_add_settings() {
    add_settings_section( 'wpturbo_section_id', __( 'Settings Section Title', 'wpturbo' ), 'wpturbo_section_callback', 'existing_page_slug' );

    add_settings_field( 'wpturbo_field_id', __( 'Field Title', 'wpturbo' ), 'wpturbo_field_callback', 'existing_page_slug', 'wpturbo_section_id', array( 'label_for' => 'wpturbo_field_id' ) );

    register_setting( 'wpturbo_settings', 'wpturbo_option_name', 'wpturbo_sanitize_callback' );
}
add_action( 'admin_init', 'WPTurbo_add_settings' );

function wpturbo_section_callback() {
    // Section HTML Output
}

function wpturbo_field_callback() {
    // Field HTML Output
}

function wpturbo_sanitize_callback( $input ) {
    // Sanitize Input
    return $sanitized_input;
}
				

The code snippet above demonstrates how to add settings to an existing page using the WordPress Settings API. This is particularly useful if you have an existing page in the WordPress admin area, and you want to add some settings to it without having to create a new page.

The first function is WPTurbo_add_settings(), which is hooked into admin_init. This function sets up the settings section and field, and registers the settings with WordPress.

The add_settings_section() function is used to create a new section in the settings page. In this example, the section is given an ID of wpturbo_section_id and a title of Settings Section Title. The third parameter, wpturbo_section_callback, is a callback function that will be used to output HTML for the section.

The add_settings_field() function adds a new field to the section. In this code snippet, we add a field with an ID of wpturbo_field_id, and a title of Field Title. The fifth parameter, existing_page_slug, is the slug of the page that we want to add the settings to. The sixth parameter, wpturbo_section_id, specifies the section ID that the field should belong to. Additionally, an array is passed as the seventh parameter, which contains any additional parameters for the input field.

Finally, we use the register_setting() function to register the settings with WordPress. In this example, we register the option with the name wpturbo_option_name. The third parameter, wpturbo_sanitize_callback, is a callback function that is used to sanitize the input before it is saved to the database.

We also define two callback functions used in this process: wpturbo_section_callback() and wpturbo_field_callback(). These functions will be used to output HTML for the section and the field, respectively. They can be used to add descriptions, input fields or any other necessary markup to your settings page.

The last callback function, wpturbo_sanitize_callback(), is responsible for sanitizing the input before it is saved to the database. Here you can validate and clean up your input before it is saved.

Overall, this code snippet provides a foundation for adding custom settings to an existing page on the WordPress admin area. With a few modifications, you can use this to add your own settings and options to any existing page.

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