When developing a custom WordPress theme or plugin, you often need a dedicated screen in the admin dashboard to manage global settings, API keys, or social links. While heavy frameworks or plugins can handle this, WordPress provides a native Settings API that allows you to build clean, secure options pages from scratch without external dependencies.
In this tutorial, we will walk through registering a custom options page, defining settings sections and fields, and securely handling form submissions.
1. Registering the Custom Options Page
First, we need to add a submenu page under the standard Settings menu using add_options_page(). We also hook into admin_init to register our settings and fields.
Add the following code to your theme’s functions.php or custom plugin:
if (!defined('ABSPATH')) {
exit;
}
// 1. Add Submenu Page under "Settings"
add_action('admin_menu', 'wg_register_custom_options_page');
function wg_register_custom_options_page() {
add_options_page(
'Theme Custom Settings', // Page title
'Theme Options', // Menu title
'manage_options', // Capability required
'wg-theme-options', // Menu slug
'wg_options_page_render_html' // Callback function to render content
);
}
2. Registering Settings and Fields (Settings API)
Next, we use register_setting(), add_settings_section(), and add_settings_field() during the admin_init hook. This ensures that WordPress automatically handles sanitization, nonces, and saving data to the wp_options table.
add_action('admin_init', 'wg_register_settings_init');
function wg_register_settings_init() {
// Register a setting with a sanitization callback
register_setting('wg_options_group', 'wg_site_settings', 'wg_options_sanitize');
// Add a section to our options page
add_settings_section(
'wg_main_section',
'General Settings',
'wg_section_callback',
'wg-theme-options'
);
// Add API Key text field
add_settings_field(
'wg_api_key',
'API Key',
'wg_api_field_callback',
'wg-theme-options',
'wg_main_section'
);
// Add Support Email text field
add_settings_field(
'wg_support_email',
'Support Email',
'wg_email_field_callback',
'wg-theme-options',
'wg_main_section'
);
}
// Section description callback
function wg_section_callback() {
echo 'Configure your global theme settings below:
';
}
// Field callbacks to render inputs
function wg_api_field_callback() {
$options = get_option('wg_site_settings');
$value = isset($options['wg_api_key']) ? $options['wg_api_key'] : '';
echo '';
}
function wg_email_field_callback() {
$options = get_option('wg_site_settings');
$value = isset($options['wg_support_email']) ? $options['wg_support_email'] : '';
echo '';
}
// Sanitization function
function wg_options_sanitize($input) {
$sanitized = [];
if (isset($input['wg_api_key'])) {
$sanitized['wg_api_key'] = sanitize_text_field($input['wg_api_key']);
}
if (isset($input['wg_support_email'])) {
$sanitized['wg_support_email'] = sanitize_email($input['wg_support_email']);
}
return $sanitized;
}
3. Rendering the Options Page HTML
Now, we create the callback function wg_options_page_render_html() to output the wrapping HTML structure, native success/error notices, and the settings form.
function wg_options_page_render_html() {
if (!current_user_can('manage_options')) {
return;
}
?>
4. Retrieving and Using Options on the Frontend
Because all fields are stored as an array inside a single option name (wg_site_settings), fetching and using them on your frontend or template files is straightforward:
$wg_options = get_option('wg_site_settings');
$api_key = isset($wg_options['wg_api_key']) ? $wg_options['wg_api_key'] : '';
if (!empty($api_key)) {
// Perform actions with the option value
}
Using the native WordPress Settings API allows you to create secure, lightweight, and standardized options pages without relying on third-party plugin frameworks. It keeps your theme or plugin fast and fully compliant with WordPress core standards.
Be sure to check out our previous tutorials on WordPress development:
How to Handle File Uploads in WordPress via AJAX & Fetch API
How to Create Custom ACF Gutenberg Blocks Without React
Building Custom Gutenberg Blocks Without Webpack: A Practical Dynamic Block Example
Need help with your project? We will be happy to help with your project. Feel free to contact us. Just reach out and we will reply soon.