Title: Using the settings API
Last modified: August 20, 2016

---

# Using the settings API

 *  [miocene22](https://wordpress.org/support/users/miocene22/)
 * (@miocene22)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/)
 * I’m trying to create a settings page for my theme using the settings API.
    For
   learning I am applying it to the twentyten. I’ve created an “options” folder 
   in the theme folder and have added an options.php which I include from functions.
   php.
 * I have the options page working and I generate the fields by using php to generate
   the html from an array of all my options fields.
 * It all works great but when I click save it just takes me to a page of all the
   possible wordpress options on one page and does not seem to save my options.
 * I’ve put my code in the pastebin. If someone could tell me if I’m missing something
   that’ll be great.
 * [http://pastebin.com/vp9GjURa](http://pastebin.com/vp9GjURa)
    sorry it’s quite
   long but I’m hoping skilled coders can figure it out.

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/using-the-settings-api/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/using-the-settings-api/page/2/?output_format=md)

 *  Thread Starter [miocene22](https://wordpress.org/support/users/miocene22/)
 * (@miocene22)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077796)
 * OK I’ve fixed the huge options page from coming up by adding `<?php settings_fields('
   sa_theme_options' ); ?>` but my problem lies with storing the options in the 
   database.
 * I borrowed some code on [this page](http://www.onedesigns.com/tutorials/how-to-create-a-wordpress-theme-options-page)
   that I believe stores all the options in one database entry in the form of an
   array.
 * Meanwhile, my options page assigns an id to each option with the intention of
   storing each option as 1 database entry.
 * Just not sure how to sort this out…
 *  Thread Starter [miocene22](https://wordpress.org/support/users/miocene22/)
 * (@miocene22)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077805)
 * OK sorry to keep bumping but I’m slowly picking up things:
    The purpose of the`
   register_setting()` function is clearer to me now. I’m just not quite sure how
   to give it the values to store in the database.
 * E.g. if I have the `$option_name` parameter (parameter 2) set to **sa_options**
   I have then, in my form, set one of my text fields to have `name="sa_options[
   sa_headerfontsize]"`.
    Am I right in thinking that `register_setting()` should
   save whatever in typed in the text field in the sa_options array as so?: `sa_options["
   sa_headerfontsize"] = "whatever is typed";`
 * I tried this but using phpmyadmin, there is a “sa_options” database entry in 
   the wp_options table but it doesn’t have anything in it.
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077810)
 * You may find [this tutorial](http://www.chipbennett.net/2011/02/17/incorporating-the-settings-api-in-wordpress-themes/)
   helpful.
 *  Thread Starter [miocene22](https://wordpress.org/support/users/miocene22/)
 * (@miocene22)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077814)
 * Thanks I have had a read over it but it is very long and complicated so I can’t
   find the specific bit I’m looking for.
 * Cna you explain how the fields in the form are put into the database. Because
   I don’t think `register_setting()` is getting the entries.
 * Here is the html that my `sa_theme_options_page()` and `sa_options_table()` generate:
   [http://pastebin.com/NB2bTuWZ](http://pastebin.com/NB2bTuWZ)
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077824)
 * The first thing I notice is, you don’t have a validation callback. You’ve specified
   one in your `register_setting()` call, but you’ve not defined it anywhere in 
   your pastebin code. (You have left `function sa_validate_options() {}` blank.
   Since the form `$input` is passed to this function, at the very least, you need
   to _return_ it, e.g.:
 *     ```
       function sa_validate_options( $input ) {
            return $input;
       }
       ```
   
 * Beyond that, you’ll have to step through your code, and find out what’s going
   wrong.
 * (Calling `var_dump( $input )`; die;`inside of`sa_validate_options()` is helpful
   to see what data are being passed _into_ the function.)
 *  Thread Starter [miocene22](https://wordpress.org/support/users/miocene22/)
 * (@miocene22)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077838)
 * Great thanks for the reply; I’m now getting somewhere.
 * Checking my database using phpmyadmin, I can see that it is now storing the options
   when I submit the form. This is having fixed my validation function as you described.
   _(
   I will implement some validation soon but I just want the concept to work first)_
 * i think you’ve answered all my problems for now, but I’ll be back if/when I stumble
   again.
 * Thanks again..
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077845)
 * **[@chip](https://wordpress.org/support/users/chip/)**: Perhaps you could add
   a link for your tutorial to the Codex someplace? I think it would be a great 
   addition.
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077847)
 * Somebody already did. 🙂
 * [http://codex.wordpress.org/Settings_API#External_References](http://codex.wordpress.org/Settings_API#External_References)
 *  Thread Starter [miocene22](https://wordpress.org/support/users/miocene22/)
 * (@miocene22)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077875)
 * OK I’ve hit a stumbling block.
 * Most is working but I’m having trouble calling the options into the theme:
 * I’m storing all the options in an array under the database entry called sa_options.
 * In my functions.php I have defined the array `$sa_settings = get_option('sa_options');`
   to allow me to call the options onto any theme page.
 * This mostly works but does not work for the header.php.
 * if I put `print_r ($sa_settings)` in header.php (within the actual body so I 
   can actually read whatever is output on the page) nothing is output.
    However,
   if I put the same function in index.php it prints the contents of the array.
 * It seems as though wordpress calls header.php before defining $sa_settings (which
   is defined in functions.php). Any idea how to ensure the $sa_settings variable
   is defined inside header.php aside from putting it there?
 *  Thread Starter [miocene22](https://wordpress.org/support/users/miocene22/)
 * (@miocene22)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077877)
 * actually, just putting `$sa_settings = get_option('sa_options');` at the top 
   of header.php is probably the best way isn’t it?
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077880)
 * > actually, just putting $sa_settings = get_option(‘sa_options’); at the top 
   > of header.php is probably the best way isn’t it?
 * You got it!
 * I’m thinking of playing around with hooking something into `wp_head`, just because
   I like to keep my `header.php` as clean as possible. But in any case, yes: you
   do need to output `$sa_settings = get_option('sa_options');` somewhere in the
   template directly.
 *  Thread Starter [miocene22](https://wordpress.org/support/users/miocene22/)
 * (@miocene22)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077882)
 * OK I’ve put it in header.php and stuff in the header works but now the $sa_settings
   seems to be null within index.php.
    Is is OK to put it in functions.php too. 
   Or how do I make the variable available everywhere?
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077887)
 * First, you should probably globalize it:
 *     ```
       global $sa_settings = get_option('sa_options');
       ```
   
 * Second, I don’t know for certain. I’ve been trying to figure out the definitive
   answer to this one. I think the answer is that, if it’s called within a function,
   it’s not going to be available outside of the function.
 * You might try adding the call to the top of your master template files (`index.
   php`, `page.php`, etc.), before the `<?php get_header; ?>` call?
 *  Thread Starter [miocene22](https://wordpress.org/support/users/miocene22/)
 * (@miocene22)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077896)
 * It’s not called within a function.
 * For simplicity I’ve put it in header.php and functions.php like so:
 *     ```
       if (!isset($sa_settings)){
       $sa_settings = get_option( 'sa_options' );
       }
       ```
   
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/#post-2077900)
 * > For simplicity I’ve put it in header.php and functions.php like so:
 * Are you globalizing it everywhere it’s called?

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/using-the-settings-api/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/using-the-settings-api/page/2/?output_format=md)

The topic ‘Using the settings API’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 16 replies
 * 3 participants
 * Last reply from: [miocene22](https://wordpress.org/support/users/miocene22/)
 * Last activity: [15 years, 1 month ago](https://wordpress.org/support/topic/using-the-settings-api/page/2/#post-2077904)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
