How to Create Custom Options Pages in WordPress Admin Without Plugins

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 '<p>Configure your global theme settings below:</p>';
}

// 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 '<input type="text" name="wg_site_settings[wg_api_key]" value="' . esc_attr($value) . '" class="regular-text" />';
}

function wg_email_field_callback() {
    $options = get_option('wg_site_settings');
    $value   = isset($options['wg_support_email']) ? $options['wg_support_email'] : '';
    echo '<input type="email" name="wg_site_settings[wg_support_email]" value="' . esc_attr($value) . '" class="regular-text" />';
}

// 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;
    }
    ?>
    <div class="wrap">
        <h1><?php echo esc_html(get_admin_page_title()); ?></h1>
        <form action="options.php" method="post">
            <?php
            // Output security nonces and hidden fields for the registered group
            settings_fields('wg_options_group');
            
            // Output sections and fields registered for this page
            do_settings_sections('wg-theme-options');
            
            // Output standard save button
            submit_button('Save Changes');
            ?>
        </form>
    </div>
    <?php
}
				
			

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.

Comments

No comments yet

Share Your Thoughts

Join the conversation!

Scroll to Top

Share Your Thoughts

Thank you for your comment!

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

Pro

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Standard

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Basic

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project