# Using the Settings API - Part 2: Create A Top Level Admin Menu

This is Part Two of "Create Theme Settings Pages that use the Settings API" tutorial. In this part, we will be looking into how we can create a top level admin menu with more than one settings pages as well as how we can add tabs when we need them. If you have not yet had the chance to read Part One, you may want to do that first before you continue. This tutorial builds on the code shown in the first part of this tutorial.

## A look at what we will be creating

Just like we did in Part One lets take a look at how the finished result looks like. We will be using the twentyeleven WordPress 3.2 default theme again however feel free to use your own theme if you prefer.

2. Find the Part Two/source_files/lib folder and upload it inside the twentyeleven theme folder so that it is on the same level as the twentyeleven/js folder you see.
3. Then, open the Part Two/source_files/functions.php in a code editor and copy the require_once code line.
4. Next, open the twentyeleven/functions.php in your code editor.
Find the twentyeleven_setup() function around line 74 and paste the line you copied earlier (point 3) inside the function as you see shown below.
This will replace the previous require_once() line of code you added in Part One.
 1 2 function twentyeleven_setup() {  3 4  //require only in admin!  5  if(is_admin()){  6  require_once('lib/wptuts-theme-settings-advanced.php');  7  } 

Upon completing this step you should be able to see the Wptuts Settings top-level menu in your Admin area. You will notice the two menu links: Options Page One and Options Page Two. Options Page One is exactly the same settings page you created in Part One so go directly to Options Page Two Following the Wptuts Settings Page Two title you see four tabs with their individual settings fields. To keep things simple and help you concentrate on the code required to create multiple pages and tabs you will notice that the settings are the same as those you see on Options Page One.

Now that you see what the finished result looks like, let's learn how we can re-create it step by step.

"The final code described in this step is found in the Part Two/source_ files/step1 folder"

## Step 1 Registering the administration pages

We will be using the same my-theme-settings.php document you were working with previously in Part One so go ahead and open that file in your code editor.

### Register the top level menu

We will edit the existing wptuts_add_menu() function by adding a call to the add_menu_page(). We will add this right after we collect our contextual help like you see done below.

 1 2 3 /*  4  * The admin menu pages  5  */  6 function wptuts_add_menu(){  7   8  $settings_output = wptuts_get_settings();  9  // collect our contextual help text  10 $wptuts_contextual_help = $settings_output['wptuts_contextual_help'];  11   12  // As a "top level" menu  13  // add_menu_page($page_title, $menu_title,$capability, $menu_slug,$function, $icon_url,$position );  14  add_menu_page( __('Wptuts Settings'), __('Wptuts Settings','wptuts_textdomain'), 'manage_options', WPTUTS_PAGE_BASENAME, 'wptuts_settings_page_fn');  15   16  // old code follows below  17 } 

Understand the add_menu_page() parameters

Take note of the add_menu_page() function parameters so you can later on customize the function call to your needs. The information is taken from the Codex page:

• $page_title - The text to be displayed in the title tag of the page when the menu is selected • $menu_title - The text to be used for the menu
• $capability - The capability required for this menu to be displayed to the user. • $menu_slug - The slug name to refer to this menu by (should be unique for this menu). (Note the use of our WPTUTS_PAGE_BASENAME constant!)
• $function - The callback function to output the content for this page. • $icon_url - The url to the icon to be used for this menu. This parameter is optional. (We did not include this paramenter)
• $position - The position in the menu order this menu should appear. (We did not include this paramenter) ### Register the pages Next, we will replace the existing add_theme_page() call with the add_submenu_page() function to register our first settings page. Then, we will register a second page as you see shown below.  1 2 /*  3  * The admin menu pages  4  */  5 function wptuts_add_menu(){  6   7 $settings_output = wptuts_get_settings();  8  // collect our contextual help text  9  $wptuts_contextual_help =$settings_output['wptuts_contextual_help'];  10   11  // As a "top level" menu  12  // add_menu_page( $page_title,$menu_title, $capability,$menu_slug, $function,$icon_url, $position );  13  add_menu_page( __('Wptuts Settings'), __('Wptuts Settings','wptuts_textdomain'), 'manage_options', WPTUTS_PAGE_BASENAME, 'wptuts_settings_page_fn');  14   15  // page one  16  // add_submenu_page($parent_slug, $page_title,$menu_title, $capability,$menu_slug, $function );  17 $wptuts_settings_page = add_submenu_page(WPTUTS_PAGE_BASENAME, __('Wptuts Settings | Options'), __('Options Page One','wptuts_textdomain'), 'manage_options', WPTUTS_PAGE_BASENAME, 'wptuts_settings_page_fn');  18  // contextual help  19  if ($wptuts_settings_page) {  20  add_contextual_help($wptuts_settings_page, $wptuts_contextual_help );  21  }  22  // css & js  23  add_action( 'load-'.$wptuts_settings_page, 'wptuts_settings_scripts' );  24   25  // page two  26  $wptuts_settings_page_two = add_submenu_page(WPTUTS_PAGE_BASENAME, __('Wptuts Settings | Options Page Two', 'wptuts_textdomain'), __('Options Page Two','wptuts_textdomain'), 'manage_options', WPTUTS_PAGE_BASENAME . '-page-two', 'wptuts_settings_page_fn');  27  // contextual help  28  if ($wptuts_settings_page_two) {  29  add_contextual_help( $wptuts_settings_page_two,$wptuts_contextual_help );  30  }  31  // css & js  32  add_action( 'load-'. $wptuts_settings_page_two, 'wptuts_settings_scripts' );  33 }  Note the variable names used for page two. When you customize this later you may want to use names that are more descriptive of the theme settings they contain. You can repeat the code we used for registering page two as many times as the number of settings pages you want to create. Take care to edit the page titles and variables accordingly. To help you in this, compare the code block for registering page one to the code block used for registering page two. Note which parameters change and which remain constant. Imitate this for any other pages you may want to create. Understand the add_submenu_page() parameters Take note of the add_submenu_page() function parameters so you can later on customize the function call to your needs. The information is taken from the Codex page: • $page_title - The text to be displayed in the title tag of the page when the menu is selected
• $menu_title - The text to be used for the menu • $capability - The capability required for this menu to be displayed to the user.
• $menu_slug - The slug name to refer to this menu by (should be unique for this menu). (Note the use of our WPTUTS_PAGE_BASENAME constant!) • $function - The callback function to output the content for this page.

### Check the result

Do you still have a copy of my-theme-options.php file from Part One in your twentyeleven/lib folder? If you don't, upload the one (my-theme-options.php) you were working with during the first part of this tutorial or the one you find in Part One/source_files/step7 folder, before you open your WordPress admin in your browser.

If you have followed the above successfully, this is how your settings page should look like at this point.
Note that the page content will be the same for both pages. This is to be expected as we have not yet defined or registered the settings for the second page. This we will do in Steps 2 and 3.

## Step 2 Defining the settings for each settings page

"The final code described in this step is found in the Part Two/source_ files/step2 folder"

Now that we have our settings pages in place we need to tell WordPress the settings, sections and fields we want to register and whitelist i.e. sanitize for each individual settings page. You may recall that the wptuts_register_settings() function (located in my-theme-settings.php) does exactly that for one settings page but how do we handle multiple pages?
And what if we want to display a page's settings in tabs?

The approach we will take is this:
Let WordPress know which page - and when using tabs, which tab - we are currently viewing and based on that register, display and save the settings needed for the current settings page.
Note that when saving the settings, we will save the settings only displayed on the page or tab we currently have open.

Especially those of you familiar with the excellent Incorporating the Settings API in WordPress Themes tutorial by Chip Bennett take extra note of this: The action of saving the settings will require that each page and tab saves it's settings under it's own unique option name (as used in a get_option() function call)! This need not necessarily be taken as a negative thing. It is simply a different approach; one that is required because we register, display and save only those settings under the current page or tab. Whether this is the best approach for you to take in a specific project can only be determined by the project at hand and the number of pages/tabs you need to create. So please, do keep an open mind as you read on.

The first thing we need is a couple of new helper functions. One function should be able to return the current settings page and another function the current tab. I've decided to keep these in a separate document so let's create that first.

### Prepare a new Document with some helper functions

Create a new document in your code editor and call it wptuts-helper-functions.php. Copy and paste in it the three functions given below then, upload it inside the twentyeleven/lib folder. Each function is explained before the next is given.

The code given below should be written in the wptuts-helper-functions.php file.

 1 2 /**  3  * Helper function: Check for pages and return the current page name  4  *  5  * @return string  6  */  7 function wptuts_get_admin_page() {  8  global $pagenow;  9   10  // read the current page  11 $current_page = trim($_GET['page']);  12   13  // use a different way to read the current page name when the form submits  14  if ($pagenow == 'options.php') {  15  // get the page name  16  $parts = explode('page=',$_POST['_wp_http_referer']); // http://codex.wordpress.org/Function_Reference/wp_referer_field  17  $page =$parts[1];  18 19  // account for the use of tabs (we do not want the tab name to be part of our return value!)  20  $t = strpos($page,"&");  21   22  if($t !== FALSE) {  23 $page = substr($parts[1],0,$t);  24  }  25   26  $current_page = trim($page);  27  }  28   29 return $current_page;  The first helper function will return the current page name. We depend on the $_GET superglobal.
The variable will return the page name we are currently viewing - e.g. wptuts-settings as shown in the ?page=wptuts-settings part of the page URL.

However, when we save the settings our $_GET variable will not be able to return the settings page name we expect (it will actually return a NULL value). Why? Well, when we save our settings WordPress will send a POST request to options.php and at that moment we no longer have ?page=wptuts-settings as part of our page URL. What now? WordPress includes the page name in the $_POST['_wp_http_referer'] variable.
Using the $pagenow WordPress global we can check when options.php is called and return the value stored in $_POST['_wp_http_referer']

 1 2 /**  3  * Helper function: Set default tab  4  *  5  * @return string  6  */  7 function wptuts_default_tab() {  8  // find current page  9  $current_page = wptuts_get_admin_page();  10   11  // There may be times when the first tab has a different slug from page to page. Here is where you can override the$default_tab = 'general'; Gives you more control :)  12  // if our current page is the 'wptuts-settings-page-two' page then set the default tab to 'text-inputs'  13  if ($current_page == 'wptuts-settings-page-two') {  14 $default_tab = 'text-inputs';  15   16  // if you have more settings pages with a first tab slug other than "general" continue with an  17  //}elseif($current_page == 'your-page-slug'){  18  //conditional here.  19   20  // else fallback to the "general" tab.  21  } else {  22 $default_tab = 'general';  23  }  24 return $default_tab;  25 }  This second helper function returns the default tab name. It it useful when your default tab slug is not always called general. To customize the function to your needs later, please take a moment to read through the comments left in the code.  1 2 /**  3  * Helper function: Check for tabs and return the current tab name  4  *  5  * @return string  6  */  7 function wptuts_get_the_tab() {  8  global$pagenow;  9   10  // set default tab  11  $default_tab = wptuts_default_tab();  12   13  // read the current tab when on our settings page  14 $current_tab = (isset($_GET['tab']) ?$_GET['tab'] : $default_tab);  15   16  //use a different way to read the tab when the form submits  17  if ($pagenow == 'options.php') {  18  // need to read the tab name so we explode()!  19  $parts = explode('&tab=',$_POST['_wp_http_referer']); // http://codex.wordpress.org/Function_Reference/wp_referer_field  20  // count the "exploded" parts  21  $partsNum = count($parts);  22   23  // account for "&settings-updated=true" (we do not want that to be part of our return value!)  24  // is it "&settings-updated=true" there? - check for the "&"  25  $settings_updated = strpos($parts[1],"&");  26   27  // filter it out and get the tab name  28  $tab_name = ($settings_updated !== FALSE ? substr($parts[1],0,$settings_updated) : $parts[1]);  29   30  // use if found, otherwise pass the default tab name  31 $current_tab = ($partsNum == 2 ? trim($tab_name) : $default_tab);  32  }  33   34 return$current_tab  35 } 

The third helper function returns the current tab name.
Similar to what we did in the wptuts_get_admin_page() function we use the $_GET superglobal for reading the current tab name when the settings display and the $_POST['_wp_http_referer'] variable for reading the tab name when the settings save.

Remember to save the three functions shown above in the wptuts-helper-functions.php and upload it inside the twentyeleven/lib folder.

### Define those settings

Time to define page tabs, settings sections and fields and contextual help for each settings page. We actually have the settings needed for "Options Page One" already from Part One of this tutorial. The sections, fields and contextual help fuctions are defined in my-theme-options.php and we will leave those as they are.
What we will do now is define settings for "Options Page Two".

The "Options Page Two" settings page will devide it's settings into tabs. Create a new document called my-theme-options-two.php then copy and paste in it the functions given below. Each function is explained after the code is given.

Define the page tabs

 1 2 /**  3  * Define page tabs  4  * $tabs['tab-slug'] = __('Tab Name', 'wpShop');  5  */  6 function wptuts_options_two_page_tabs() {  7   8 $tabs = array();  9   10  $tabs['text-inputs'] = __('Text Inputs', 'wpShop');  11 $tabs['textareas'] = __('Textareas', 'wpShop');  12  $tabs['select'] = __('Select', 'wpShop');  13 $tabs['checkboxes'] = __('Checkboxes', 'wpShop');  14   15  return $tabs;  16 }  The tab array should have as value, the tab name as it will appear on each tab and as key, the tab slug as it will appear as part of the page URL as in &tab=tab-name Define settings sections for each tab  1 2 /**  3  * Define our settings sections  4  *  5  * array key=$id, array value=$title in: add_settings_section($id, $title,$callback, $page );  6  * @return array  7  */  8 function wptuts_options_two_page_sections() {  9  // we change the output based on open tab  10   11  // get the current tab  12 $tab = wptuts_get_the_tab();  13 14  // switch sections array according to tab  15  switch ($tab) {  16  // Text Inputs  17  case 'text-inputs':  18 $sections = array();  19  $sections['txt_section'] = __('Text Form Fields', 'wptuts_textdomain');  20  break;  21   22  // Textareas  23  case 'textareas':  24 $sections = array();  25  $sections['txtarea_section'] = __('Textarea Form Fields', 'wptuts_textdomain');  26  break;  27   28  // Select  29  case 'select':  30 $sections = array();  31  $sections['select_section'] = __('Select Form Fields', 'wptuts_textdomain');  32  break;  33   34  // Checkboxes  35  case 'checkboxes':  36 $sections = array();  37  $sections['checkbox_section'] = __('Checkbox Form Fields', 'wptuts_textdomain');  38  break;  39  }  40   41 return$sections;  42 } 

The sections array is much the same as we defined it for "Options Page One" - my-theme-options.php - in that it returns an associative array where the value, is the section title and the key, the section id. However, the sections array output changes according to the current open tab. Note the tab switch.

 1 2 // get the current tab  3 $tab = wptuts_get_the_tab();  4 5 // switch sections array according to tab  6  switch ($tab) {  7  // Text Inputs  8  case 'text-inputs':  9   10  break;  11   12  // Textareas  13  case 'textareas':  14   15  break;  16   17  // Select  18  case 'select':  19   20  break;  21   22  // Checkboxes  23  case 'checkboxes':  24   25  break;  26 } 

Each case is the tab slug - the array key from our wptuts_options_two_page_tabs() function. When you customize this function later you will need to create a new case for each tab you define.

The wptuts_get_the_tab() function you see called before the switch was defined in our wptuts-helper-functions.php. It helps us "read" the current tab name - the &tab=tab-name part of the page URL and passes that on to the $tab variable.  1 2 // get the current tab  3 $tab = wptuts_get_the_tab(); 

Define settings fields for each tab

 1 2 /**  3  * Define our form fields (options)  4  *  5  * @return array  6  */  7 function wptuts_options_two_page_fields() {  8   9  // get the tab using wptuts_get_the_tab() for we need options.php to be able to process the form submission!  10  $tab = wptuts_get_the_tab();  11   12  // setting fields according to tab  13  switch ($tab) {  14  // Text Inputs  15  case 'text-inputs':  16  $options[] = array(  17  "section" => "txt_section",  18  "id" => WPTUTS_SHORTNAME . "_txt_input_2",  19  "title" => __( 'Text Input - Some HTML OK!', 'wptuts_textdomain' ),  20  "desc" => __( 'A regular text input field. Some inline HTML (<a>, <b>, <em>, <i>, <strong>) is allowed.', 'wptuts_textdomain' ),  21  "type" => "text",  22  "std" => __('Some default value','wptuts_textdomain')  23  );  24   25 $options[] = array(  26  "section" => "txt_section",  27  "id" => WPTUTS_SHORTNAME . "_nohtml_txt_input_2",  28  "title" => __( 'No HTML!', 'wptuts_textdomain' ),  29  "desc" => __( 'A text input field where no html input is allowed.', 'wptuts_textdomain' ),  30  "type" => "text",  31  "std" => __('Some default value','wptuts_textdomain'),  32  "class" => "nohtml"  33  );  34   35  $options[] = array(  36  "section" => "txt_section",  37  "id" => WPTUTS_SHORTNAME . "_numeric_txt_input_2",  38  "title" => __( 'Numeric Input', 'wptuts_textdomain' ),  39  "desc" => __( 'A text input field where only numeric input is allowed.', 'wptuts_textdomain' ),  40  "type" => "text",  41  "std" => "123",  42  "class" => "numeric"  43  );  44   45 $options[] = array(  46  "section" => "txt_section",  47  "id" => WPTUTS_SHORTNAME . "_multinumeric_txt_input_2",  48  "title" => __( 'Multinumeric Input', 'wptuts_textdomain' ),  49  "desc" => __( 'A text input field where only multible numeric input (i.e. comma separated numeric values) is allowed.', 'wptuts_textdomain' ),  50  "type" => "text",  51  "std" => "123,234,345",  52  "class" => "multinumeric"  53  );  54   55  $options[] = array(  56  "section" => "txt_section",  57  "id" => WPTUTS_SHORTNAME . "_url_txt_input_2",  58  "title" => __( 'URL Input', 'wptuts_textdomain' ),  59  "desc" => __( 'A text input field which can be used for urls.', 'wptuts_textdomain' ),  60  "type" => "text",  61  "std" => "https://code.tutsplus.com",  62  "class" => "url"  63  );  64   65 $options[] = array(  66  "section" => "txt_section",  67  "id" => WPTUTS_SHORTNAME . "_email_txt_input_2",  68  "title" => __( 'Email Input', 'wptuts_textdomain' ),  69  "desc" => __( 'A text input field which can be used for email input.', 'wptuts_textdomain' ),  70  "type" => "text",  71  "std" => "email@email.com",  72  "class" => "email"  73  );  74   75  $options[] = array(  76  "section" => "txt_section",  77  "id" => WPTUTS_SHORTNAME . "_multi_txt_input_2",  78  "title" => __( 'Multi-Text Inputs', 'wptuts_textdomain' ),  79  "desc" => __( 'A group of text input fields', 'wptuts_textdomain' ),  80  "type" => "multi-text",  81  "choices" => array( __('Text input 1','wptuts_textdomain') . "|txt_input1", __('Text input 2','wptuts_textdomain') . "|txt_input2", __('Text input 3','wptuts_textdomain') . "|txt_input3", __('Text input 4','wptuts_textdomain') . "|txt_input4"),  82  "std" => ""  83  );  84  break;  85   86  // Textareas  87  case 'textareas':  88 $options[] = array(  89  "section" => "txtarea_section",  90  "id" => WPTUTS_SHORTNAME . "_txtarea_input_2",  91  "title" => __( 'Textarea - HTML OK!', 'wptuts_textdomain' ),  92  "desc" => __( 'A textarea for a block of text. HTML tags allowed!', 'wptuts_textdomain' ),  93  "type" => "textarea",  94  "std" => __('Some default value','wptuts_textdomain')  95  );  96 97  $options[] = array(  98  "section" => "txtarea_section",  99  "id" => WPTUTS_SHORTNAME . "_nohtml_txtarea_input_2",  100  "title" => __( 'No HTML!', 'wptuts_textdomain' ),  101  "desc" => __( 'A textarea for a block of text. No HTML!', 'wptuts_textdomain' ),  102  "type" => "textarea",  103  "std" => __('Some default value','wptuts_textdomain'),  104  "class" => "nohtml"  105  );  106   107 $options[] = array(  108  "section" => "txtarea_section",  109  "id" => WPTUTS_SHORTNAME . "_allowlinebreaks_txtarea_input_2",  110  "title" => __( 'No HTML! Line breaks OK!', 'wptuts_textdomain' ),  111  "desc" => __( 'No HTML! Line breaks allowed!', 'wptuts_textdomain' ),  112  "type" => "textarea",  113  "std" => __('Some default value','wptuts_textdomain'),  114  "class" => "allowlinebreaks"  115  );  116 117  $options[] = array(  118  "section" => "txtarea_section",  119  "id" => WPTUTS_SHORTNAME . "_inlinehtml_txtarea_input_2",  120  "title" => __( 'Some inline HTML ONLY!', 'wptuts_textdomain' ),  121  "desc" => __( 'A textarea for a block of text.  122  Only some inline HTML  123  (<a>, <b>, <em>, <strong>, <abbr>, <acronym>, <blockquote>, <cite>, <code>, <del>, <q>, <strike>)  124  is allowed!', 'wptuts_textdomain' ),  125  "type" => "textarea",  126  "std" => __('Some default value','wptuts_textdomain'),  127  "class" => "inlinehtml"  128  );  129  break;  130   131  // Select  132  case 'select':  133 $options[] = array(  134  "section" => "select_section",  135  "id" => WPTUTS_SHORTNAME . "_select_input_2",  136  "title" => __( 'Select (type one)', 'wptuts_textdomain' ),  137  "desc" => __( 'A regular select form field', 'wptuts_textdomain' ),  138  "type" => "select",  139  "std" => "3",  140  "choices" => array( "1", "2", "3")  141  );  142   143  $options[] = array(  144  "section" => "select_section",  145  "id" => WPTUTS_SHORTNAME . "_select2_input_2",  146  "title" => __( 'Select (type two)', 'wptuts_textdomain' ),  147  "desc" => __( 'A select field with a label for the option and a corresponding value.', 'wptuts_textdomain' ),  148  "type" => "select2",  149  "std" => "",  150  "choices" => array( __('Option 1','wptuts_textdomain') . "|opt1", __('Option 2','wptuts_textdomain') . "|opt2", __('Option 3','wptuts_textdomain') . "|opt3", __('Option 4','wptuts_textdomain') . "|opt4")  151  );  152  break;  153   154  // Checkboxes  155  case 'checkboxes':  156 $options[] = array(  157  "section" => "checkbox_section",  158  "id" => WPTUTS_SHORTNAME . "_checkbox_input_2",  159  "title" => __( 'Checkbox', 'wptuts_textdomain' ),  160  "desc" => __( 'Some Description', 'wptuts_textdomain' ),  161  "type" => "checkbox",  162  "std" => 1 // 0 for off  163  );  164   165  $options[] = array(  166  "section" => "checkbox_section",  167  "id" => WPTUTS_SHORTNAME . "_multicheckbox_inputs_2",  168  "title" => __( 'Multi-Checkbox', 'wptuts_textdomain' ),  169  "desc" => __( 'Some Description', 'wptuts_textdomain' ),  170  "type" => "multi-checkbox",  171  "std" => 0,  172  "choices" => array( __('Checkbox 1','wptuts_textdomain') . "|chckbx1", __('Checkbox 2','wptuts_textdomain') . "|chckbx2", __('Checkbox 3','wptuts_textdomain') . "|chckbx3", __('Checkbox 4','wptuts_textdomain') . "|chckbx4")  173  );  174  break;  175  }  176   177  return$options;  178 } 

The fields array is much same as we defined it for "Options Page One". You will notice that I even left the $options[] = array() arguments the same with the option id the only exception (I simply added a _2) The important point to take home here is that the fields array output changes according to the current open tab like we did in our wptuts_options_two_page_sections() function. Note the tab switch.  1 2 // get the current tab  3 $tab = wptuts_get_the_tab();  4 5 // setting fields according to tab  6  switch ($tab) {  7  // Text Inputs  8  case 'text-inputs':  9   10  break;  11   12  // Textareas  13  case 'textareas':  14   15  break;  16   17  // Select  18  case 'select':  19   20  break;  21   22  // Checkboxes  23  case 'checkboxes':  24   25  break;  26 }  Each case is the tab slug - the array keys from our wptuts_options_two_page_tabs() function. When you customize this function later you will need to create a new case for each tab you define. Again, you see the wptuts_get_the_tab() function called before the switch. It helps us "read" the current tab name - the &tab=tab-name part of the page URL and passes that on to the $tab variable.

 1 2 // get the current tab  3 $tab = wptuts_get_the_tab();  Define contextual help for each tab Following the same pattern of switching the array output according to the current open tab, we define each tab's contextual help.  1 2 /**  3  * Contextual Help  4  */  5 function wptuts_options_two_page_contextual_help() {  6  // get the current tab  7 $tab = wptuts_get_the_tab();  8   9  $text = "<h3>" . __('Wptuts Settings Page Two - Contextual Help','wptuts_textdomain') . "</h3>";  10   11  // contextual help according to tab  12  switch ($tab) {  13  // Text Inputs  14  case 'text-inputs':  15  $text .= "<p>" . __('Contextual help for the "Text Input" settings fields goes here.','wptuts_textdomain') . "</p>";  16  break;  17   18  // Textareas  19  case 'textareas':  20 $text .= "<p>" . __('Contextual help for the "Textarea" settings fields goes here.','wptuts_textdomain') . "</p>";  21  break;  22   23  // Select  24  case 'select':  25  $text .= "<p>" . __('Contextual help for the "Select" settings fields goes here.','wptuts_textdomain') . "</p>";  26  break;  27   28  // Checkboxes  29  case 'checkboxes':  30 $text .= "<p>" . __('Contextual help for the "Checkboxes" settings fields goes here.','wptuts_textdomain') . "</p>";  31  break;  32  }  33   34  // must return text! NOT echo  35  return $text;  36 }  All the settings code given above should be now written in your my-theme-options-two.php file. Save it and upload it inside the twentyeleven/lib folder ## Step 3 Registering the tabs and settings for each settings page "The final code described in this step is found in the Part Two/source_ files/step3 folder" Now that our page settings are all nicely defined, let's tell WordPress to display them. ### Include the new Documents we created in Step 2 We want to use our new helper functions and settings in our my-theme-settings.php file so let's include the new wptuts-helper-functions.php and my-theme-options-two.php files. Find the existing require_once('my-theme-options.php'); code line and adjust as you see below. The code given below should be written in the my-theme-settings.php file.  1 2 /**  3  * Include the required files  4  */  5 // helper functions  6 require_once('wptuts-helper-functions.php');  7 // page settings sections & fields as well as the contextual help text.  8 require_once('wptuts-theme-options.php');  9 require_once('wptuts-theme-options-two.php');  ### Adjust the wptuts_get_settings() function Time to adjust the existing wptuts_get_settings() helper function to retrieve our settings based on the page or tab we currently have open.  1 2 /**  3  * Helper function for defining variables for the current page  4  *  5  * @return array  6  */  7 function wptuts_get_settings() {  8   9 $output = array();  10   11  /*PAGES*/  12  // get current page  13  $page = wptuts_get_admin_page();  14   15  /*TABS*/  16  // get current tab  17 $tab = wptuts_get_the_tab();  18   19  /*DEFINE VARS*/  20  // define variables according to registered admin menu page  21  switch ($page) {  22  case WPTUTS_PAGE_BASENAME:  23 $wptuts_option_name = 'wptuts_options';  24  $wptuts_settings_page_title = __( 'Wptuts Settings Page','wptuts_textdomain');  25 $wptuts_page_sections = wptuts_options_page_sections();  26  $wptuts_page_fields = wptuts_options_page_fields();  27 $wptuts_contextual_help = wptuts_options_page_contextual_help();  28  $wptuts_page_tabs = '';  29  break;  30   31  case WPTUTS_PAGE_BASENAME . '-page-two':  32 $wptuts_option_name = 'wptuts_options_two';  33  $wptuts_settings_page_title = __( 'Wptuts Settings Page Two','wptuts_textdomain');  34 $wptuts_page_sections = wptuts_options_two_page_sections();  35  $wptuts_page_fields = wptuts_options_two_page_fields();  36 $wptuts_contextual_help = wptuts_options_two_page_contextual_help();  37  $wptuts_page_tabs = wptuts_options_two_page_tabs();  38   39  // define a new option name according to tab  40  switch ($tab) {  41  // Text Inputs  42  case 'text-inputs':  43  $wptuts_option_name =$wptuts_option_name . '_text_inputs';  44  break;  45   46  // Textareas  47  case 'textareas':  48  $wptuts_option_name =$wptuts_option_name . '_textareas';  49  break;  50   51  // Select  52  case 'select':  53  $wptuts_option_name =$wptuts_option_name . '_select';  54  break;  55   56  // Checkboxes  57  case 'checkboxes':  58  $wptuts_option_name =$wptuts_option_name . '_checkboxes';  59  break;  60  }  61  break;  62  }  63   64  // put together the output array  65  $output['wptuts_option_name'] =$wptuts_option_name;  66  $output['wptuts_page_title'] =$wptuts_settings_page_title;  67  $output['wptuts_page_tabs'] =$wptuts_page_tabs;  68  $output['wptuts_page_sections'] =$wptuts_page_sections;  69  $output['wptuts_page_fields'] =$wptuts_page_fields;  70  $output['wptuts_contextual_help'] =$wptuts_contextual_help;  71   72   73 return $output;  74 }  Breaking-down the code We fill our $output array with the option name, page title, page tabs, settings sections, settings fields and contextual help. The values of the six array keys change based on the settings page or tab we currently have open.

First we have a page switch

 1 2 switch ($page) {  3  case WPTUTS_PAGE_BASENAME:  4   5  break;  6 7  case WPTUTS_PAGE_BASENAME . '-page-two':  8   9  break;  10 }  You will need to create a new case for each settings page you register in the wptuts_add_menu() function. (Note that the case must match the $menu_slug parameter in the add_submenu_page() function call.)

Each case defines the option name and page title with the appropriate string value. The page tabs, settings sections, settings fields and contextual help variables are defined by calling the appropriate functions that give the corresponding output. (You recall that the functions are defined in a separate document - my-theme-options.php for "Options Page One" and my-theme-options-two.php for "Options Page Two".)

Should a particular settings page not need any tabs then the $wptuts_page_tabs variable is left empty. This is done for example for "Options Page One"  1 2 case WPTUTS_PAGE_BASENAME:  3 $wptuts_option_name = 'wptuts_options';  4  $wptuts_settings_page_title = __( 'Wptuts Settings Page','wptuts_textdomain');  5 $wptuts_page_sections = wptuts_options_page_sections();  6  $wptuts_page_fields = wptuts_options_page_fields();  7 $wptuts_contextual_help = wptuts_options_page_contextual_help();  8  $wptuts_page_tabs = '';  9 break;  Finally we have a tab switch  1 2 // define a new option name according to tab  3 switch ($tab) {  4  // Text Inputs  5  case 'text-inputs':  6  $wptuts_option_name =$wptuts_option_name . '_text_inputs';  7  break;  8   9  // Textareas  10  case 'textareas':  11  $wptuts_option_name =$wptuts_option_name . '_textareas';  12  break;  13   14  // Select  15  case 'select':  16  $wptuts_option_name =$wptuts_option_name . '_select';  17  break;  18   19  // Checkboxes  20  case 'checkboxes':  21  $wptuts_option_name =$wptuts_option_name . '_checkboxes';  22  break;  23 } 

This is only needed on settings pages that we want to use tabs. You will need to create a new case for each tab you register. (Note that the case must match the tab-slug in your tab array key.) Each case defines a new option name with the appropriate string value.

### Adjust the page content output function

Finally for this step, we need to make a little adjustment to our wptuts_settings_page_fn()() function so that it displays our tabs when these are defined. Replace the existing function with the following:

 1 2 /*  3  * Admin Settings Page HTML  4  *  5  * @return echoes output  6  */  7 function wptuts_settings_page_fn() {  8  // get the settings sections array  9  $settings_output = wptuts_get_settings();  10 ?>  11  <div class="wrap">  12  <?php  13  // dislays the page title and tabs (if needed)  14  wptuts_settings_page_header();  15  ?>  16   17  <form action="options.php" method="post">  18  <?php  19  // http://codex.wordpress.org/Function_Reference/settings_fields  20  settings_fields($settings_output['wptuts_option_name']);  21  // http://codex.wordpress.org/Function_Reference/do_settings_sections  22  do_settings_sections(__FILE__);  23  ?>  24   25  <p class="submit">  26  <input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes','wptuts_textdomain'); ?>" />  27  </p>  28   29  </form>  30  </div>  31 <?php } 

We call a new function called wptuts_settings_page_header(). Let's go ahead and define this in our wptuts-helper-functions.php file. Copy and paste the following after the wptuts_get_the_tab() function.

 1 2 /**  3  * Helper function: Creates settings page title and tabs (if needed)  4  *  5  * @return echos output  6  */  7 function wptuts_settings_page_header() {  8   9  // get the tabs  10  $settings_output = wptuts_get_settings();  11 $tabs = $settings_output['wptuts_page_tabs'];  12   13  // get the current tab  14 $current_tab = wptuts_get_the_tab();  15   16  // display the icon and page title  17  echo '<div id="icon-options-general" class="icon32"><br /></div>';  18  echo '<h2>' . $settings_output['wptuts_page_title'] . '</h2>';  19   20  // check for tabs  21  if ($tabs !='') {  22  // wrap each in anchor html tags  23  $links = array();  24  foreach($tabs as $tab =>$name ) {  25  // set anchor class  26  $class = ($tab == $current_tab ? 'nav-tab nav-tab-active' : 'nav-tab');  27 $page = $_GET['page'];  28  // the link  29 $links[] = "<a class='$class' href='?page=$page&tab=$tab'>$name</a>";  30  }  31   32  echo '<h3 class="nav-tab-wrapper">';  33  foreach ( $links as$link ) {  34  echo $link;  35  }  36  echo '</h3>';  37  }  38 }  Breaking-down the code We collect our tabs by calling the wptuts_get_settings() helper function.  1 2 // get the tabs  3 $settings_output = wptuts_get_settings();  4 $tabs =$settings_output['wptuts_page_tabs']; 

Then, we get the current tab with the wptuts_get_the_tab() helper function.

 1 2 // get the current tab  3 $current_tab = wptuts_get_the_tab();  We echo the icon and page title.  1 2 // display the icon and page title  3 echo '<div id="icon-options-general" class="icon32"><br /></div>';  4 echo '<h2>' .$settings_output['wptuts_page_title'] . '</h2>'; 

If we have tabs defined, we display them too.

 1 2 // check for tabs  3 if ($tabs !='') {  4  // wrap each in anchor html tags  5 $links = array();  6  foreach( $tabs as$tab => $name ) {  7  // set anchor class  8 $class = ($tab ==$current_tab ? 'nav-tab nav-tab-active' : 'nav-tab');  9  $page =$_GET['page'];  10  // the link  11  $links[] = "<a class='$class' href='?page=$page&tab=$tab'>$name</a>";  12  }  13   14  echo '<h3 class="nav-tab-wrapper">';  15  foreach ($links as $link ) {  16  echo$link;  17  }  18  echo '</h3>';  19 } 

The tab code should be familiar to those of you who went through the
Incorporating the Settings API in WordPress Themes tutorial by Chip Bennett

### Check the result

If you have followed the above successfully, your "Options Page Two" should display it's tabs like you see below.

## Collecting the theme options together for use in our theme

The last thing we need to do before we conclude Part Two of this tutorial is to collect together the theme options we saved for each settings page and tab in our $wptuts_option variable. To do this we need to adjust the wptuts_get_global_options() function we wrote in twentyeleven/functions.php in Part One  1 2 /**  3  * Collects our theme options  4  *  5  * @return array  6  */  7 function wptuts_get_global_options(){  8   9 $wptuts_option = array();  10 11  // collect option names as declared in wptuts_get_settings()  12  $wptuts_option_names = array (  13  'wptuts_options',  14  'wptuts_options_two_text_inputs',  15  'wptuts_options_two_textareas',  16  'wptuts_options_two_select',  17  'wptuts_options_two_checkboxes'  18  );  19 20  // loop for get_option  21  foreach ($wptuts_option_names as $wptuts_option_name) {  22  if (get_option($wptuts_option_name)!= FALSE) {  23  $option = get_option($wptuts_option_name);  24   25  // now merge in main $wptuts_option array!  26 $wptuts_option = array_merge($wptuts_option,$option);  27  }  28  }  29   30 return $wptuts_option;  31 }  Breaking-down the code We take each option name we defined per page and tab. .  1 2 // collect option names as declared in wptuts_get_settings()  3 $wptuts_option_names = array (  4  'wptuts_options',  5  'wptuts_options_two_text_inputs',  6  'wptuts_options_two_textareas',  7  'wptuts_options_two_select',  8  'wptuts_options_two_checkboxes'  9 ); 

We then call the get_option() function for each and merge them in one big array which we then output

.

 1 2 // loop for get_option  3  foreach ($wptuts_option_names as$wptuts_option_name) {  4  if (get_option($wptuts_option_name)!= FALSE) {  5 $option = get_option($wptuts_option_name);  6   7  // now merge in main$wptuts_option array!  8  $wptuts_option = array_merge($wptuts_option, $option);  9  }  10  }  As mentioned in Part One, echo a particular option in any of your theme templates like this: &lt;?php echo$wptuts_option['wptuts_txt_input']; ?> - the value in the brackets is the id of the option you want to display.

This concludes Part Two of our tutorial. Hope you enjoyed reading it!