Theme Options/Settings Page in WordPress (Add Menu Pages)

WordPress comes with a number of functions and hooks that allow developers to extend its functionality. Some of these functions allow us to add menu pages to the admin menu. Let's discover which functions we can use and how to use them to add custom pages in WordPress.

Theme Options/Settings Page in WordPress

WordPress is a free open-source CMS (Content Management System) that was first developed as a blogging platform. It also offers the possibility to extend almost every functionality of a website through support for plugins, built-in hooks, and filters. This post demonstrates how to add a WordPress custom admin page to store different configurations/settings of a theme without using the Settings API.

Why Create a Custom Theme Options Page?

WordPress has its default tables to store website content, user information, settings, and other data. Sometimes we need to store some data that is globally available throughout the website and is not associated with specific content. This is where creating a custom theme options page can be beneficial to save data like social links, contact numbers, and address location.

WordPress offers different functions to add custom admin pages under different menus. We will add a page under the "Appearance" menu to demonstrate how to use the options table to save theme configurations.

  • add_comments_page: Adds a page under the "Comments" menu.
  • add_dashboard_page: Adds a page under the "Dashboard" menu.
  • add_links_page: Adds a page under the "Links" menu.
  • add_management_page: Adds a page under the "Tools" menu.
  • add_media_page: Adds a page under the "Media" menu.
  • add_options_page: Adds a page under the "Settings" menu.
  • add_pages_page: Adds a page under the "Pages" menu.
  • add_plugins_page: Adds a page under the "Plugins" menu.
  • add_posts_page: Adds a page under the "Posts" menu.
  • add_theme_page: Adds a page under the "Appearance" menu.
  • add_users_page: Adds a page under the "User" menu.
 

How to Add a Menu Page in WordPress Admin Menu? (Theme Options)

WordPress comes with a built-in Settings API that allows us to create different administrative pages. We will use a simpler approach that is ideal for creating pages that store data specific to a theme. We will add a custom page using the add_theme_page function and render HTML on the page with the input fields for social links as an example. This page will appear under the "Appearance" menu in WordPress' admin sidebar.

add_theme_page('page_title', 'menu_title', 'capability', 'menu_slug', 'callback_function');

The following is an explanation of each parameter this function accepts:

  • page_title: String to show the title of the page.
  • menu_title: String to show in the menu.
  • capability: The capability of the user required to access this page.
  • menu_slug: A unique slug for this menu to identify.
  • callback_function: A function to render the HTML content of the page.

We will create a theme-options.php file that will contain code to add the page to the sidebar. We will include this file in the theme's functions.php file. The following steps are involved to achieve this:

  • Add a hook to admin_menu with the theme_options_page function.
  • Add a hook to add_theme_page in this function to add a page under the "Appearance" menu.
  • Add the parameters for "Page title", "Menu title", "Capability", "Slug", and "Callback function".
  • Render the HTML form in the callback render_theme_options function we provided.
  • Add Bootstrap CSS and JS using the wp_enqueue_style and wp_enqueue_script hooks.
  • Add wp_nonce_field to our form.

theme-options.php

<?php
// Add themes option page to "Appearance" menu item
add_action('admin_menu', 'theme_options_page');
function theme_options_page(): void
{
add_theme_page(__('Theme Options', 'text-domain'), __('Theme Options', 'text-domain'), 'manage_options', 'theme-options', 'render_theme_options');
}

// Function that contains the form content for theme options page
function render_theme_options(): void
{
// Adding Bootstrap for our form styling
wp_enqueue_style('bootstrap', get_template_directory_uri() . '/path-to-bootstrap-directory/css/bootstrap.min.css');
wp_enqueue_script('bootstrap', get_template_directory_uri() . '/path-to-bootstrap-directory/js/bootstrap.min.js');
?>
<div class="wrap">
<h1>Theme Options</h1>
<form method="post" action="options.php" class="form form-horizontal">
<?php
wp_nonce_field('update-options');
global $theme_options;
?>
<input type="hidden" name="action" value="update"/>
<input type="hidden" name="page_options" value="theme_options"/>
<div class="form-group">

<div class="col-sm-6">
<label><?php _e('Facebook', 'text-domain'); ?></label>
<input class="form-control" name="theme_options[facebook_link]"
value="<?= $theme_options['facebook_link'] ?? null; ?>"/>
</div>
<div class="col-sm-6">
<label><?php _e('Twitter', 'text-domain'); ?></label>
<input class="form-control" name="theme_options[twitter_link]"
value="<?= $theme_options['twitter_link'] ?? null; ?>"/>
</div>
<div class="col-sm-6">
<label><?php _e('LinkedIn', 'text-domain'); ?></label>
<input class="form-control" name="theme_options[linkedin_link]"
value="<?= $theme_options['linkedin_link'] ?? null; ?>"/>
</div>
<div class="col-sm-6">
<label><?php _e('Pinterest', 'text-domain'); ?></label>
<input class="form-control" name="theme_options[pinterest_link]"
value="<?= ['pinterest_link'] ?? null; ?>"/>
</div>
</div>
<?php submit_button(__('Save Theme Options', 'text-domain')); ?>
</form>
</div>
<?php
}

Set $theme_options as a global variable and include theme-options.php in the functions.php file. We can retrieve theme options data using the get_option() function.

global $theme_options; // Set theme options global

$theme_options = get_option('theme_options'); // Get theme options

include('theme-options.php');
Theme Options Page

Adding a theme/settings page is a great way to manage data specific to a theme from one place. We demonstrated a simple approach to adding such pages with ease. The code can be modified to add more tabs/sections and fields for general settings, header settings, footer settings, typography and color settings, and more.