Introduction to Settings API

Settings API

It is the back-bone of plugin development. It was first added in WordPress 2.7 and allows semi-automatic management of admin pages containing settings forms to be managed. It lets you define settings pages, where sections can be defined within those pages and fields within those sections.

Let’s take an example:

A function ‘wp_travel_engine_settings_page‘ is created where a sub-menu page ‘WP Travel Engine Admin Settings‘ is defined. This submenu page is added to a post-type called ‘trip’.
public function wp_travel_engine_settings_page() {
add_submenu_page('edit.php?post_type=trip', 'WP Travel Engine Admin Settings', 'WP Travel Engine Settings', 'manage_options', basename(__FILE__), array($this,'wp_travel_engine_callback_function'));
add_action( 'admin_menu', 'wp_travel_engine_settings_page' );

Next, another function called ‘wp_travel_engine_register_settings‘ by hooking it to ‘admin_init’ is created where a settings called ‘wp_travel_engine_settings‘ with settings fields ‘wp_travel_engine_settings‘ is registered. For validation, third parameter (say, wp_travel_engine_validate_function) can be passed where function name is defined

public function wp_travel_engine_register_settings(){
//The third parameter is a function that will validate input values.
register_setting('wp_travel_engine_settings', 'wp_travel_engine_settings','wp_travel_engine_validate_function');
add_action( 'admin_init', 'wp_travel_engine_register_settings' );

In a nut-shell, Settings API is the integral part of plugin development where different fields with sections can be created and hence plugin settings can be easily stored.

Leave comment

Your email address will not be published. Required fields are marked with *.