Title: Multi-dimensional options array (for themes/plugins) with Settings API
Last modified: August 21, 2016

---

# Multi-dimensional options array (for themes/plugins) with Settings API

 *  [Luke](https://wordpress.org/support/users/lukejanicke/)
 * (@lukejanicke)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/multi-dimensional-options-array-for-themesplugins-with-settings-api/)
 * I just figured this out and I thought I’d share it.
 * All the blog/forum posts on this topic are more than a year old. Either people
   have given up or I haven’t found the official post that explains how to do this.
   If there’s a more recent one out there, please share it with me.
 * NOTE: A few posts says to use `serialize()` and `unserialize()`. You can but 
   you don’t have to. There is an easier way. Read on.
 * This is a generalized/simplified sample of how I store theme settings in a multidimensional
   array.
 * First, populate with some defaults. This step is optional but it shows you how
   I have **_an array within an array_**.
 *     ```
       function set_default_theme_settings() {
       	$meta = array (
       		'date' => 1,
       		'category' => 1,
       		'author' => 1,
       		'comments' => 1
       	);
       	$options = array (
       		'copyright' => 'blah blah blah',
       		'notfound' => 'yadda yadda yadda',
       		'meta' => $meta // This is the array within an array.
       	);
       	add_option( 'blah', $options );
       }
       add_action( 'after_theme_setup', 'set_default_theme_settings' );
       ```
   
 * `blah` is the name of my theme (not in real life) and here I use it as the name
   of the option. Of course, change `blah` to something unique for your theme/plugin.
   Probably the name of your own theme or plugin.
 * I won’t post all the code.
 * Cut a long story short, inside the form you pull out the options.
 * `$options = get_option('blah');`
 * Then you can use them like this:
 * `<input type="text" name="blah[copyright]" value="<?php echo $options['copyright'];?
   >" />`
 * `<input type="checkbox" id="meta_date" name="blah[meta][date]" value="1" <?php
   echo checked( 1, $options['meta']['date'], false ); ?> />`
 * Take careful note of where to use `blah['…']` (for storing) and where to use `
   $options['…']` (for retrieving).
 * You can retrieve and store in the second array just by doubling up the square
   brackets…
 * `options['meta']['date']`
    `blah[meta][date]`
 * And I _assume_ you can bury data in endless depths of embedded arrays:
 * `blah[program][department][section][project][methods][tools]…`
 * This is basic PHP syntax, but for me the key was realizing that I could use this
   PHP syntax in the HTML input name attribute for saving to the options multi-dimensional
   array using the WordPress Settings API. And WordPress would handle it all for
   me. Sweet!
 * i.e. `name="blah[meta][date]"`
 * That’s all folks! That’s all you need to do to retrieve and store settings in
   a multi-dimensional array in a single WordPress option.
 * P.S. If this is already common knowledge, please point me in the direction of
   some good references. I figured it out by trial and error, intuition and blunder.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [PromInc](https://wordpress.org/support/users/prominc/)
 * (@prominc)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/multi-dimensional-options-array-for-themesplugins-with-settings-api/#post-4552068)
 * You sir, are a life saver.
 * You are right, that it’s a simple PHP array syntax to retrieve the data. The 
   key for me here was that I could use that same PHP syntax as the name field and
   that would save back to the WP database for me. That saved me a huge amount of
   headache and work for how I figured I’d have to solve this problem…
 * Thanks so much for posting this! If this information is elsewhere on the internet,
   I wasn’t able to find it.
 *  Thread Starter [Luke](https://wordpress.org/support/users/lukejanicke/)
 * (@lukejanicke)
 * [11 years, 8 months ago](https://wordpress.org/support/topic/multi-dimensional-options-array-for-themesplugins-with-settings-api/#post-4552069)
 * Great! Nice to know it helped someone.
 *  [Yudhistira Mauris](https://wordpress.org/support/users/maurisrx/)
 * (@maurisrx)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/multi-dimensional-options-array-for-themesplugins-with-settings-api/#post-4552076)
 * Thank you very much! This is what I’ve been searching for two hours. I’m new 
   to multi-dimensional array and this really has helped me a lot.
 *  [Yudhistira Mauris](https://wordpress.org/support/users/maurisrx/)
 * (@maurisrx)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/multi-dimensional-options-array-for-themesplugins-with-settings-api/#post-4552077)
 * I want to add something.If you want to save multi-dimensional array from select
   element, you can do it like this:
 *     ```
       <select id="select_element" name="blah[meta][]" >
       <option></option>
       <option></option>
       </select>
       ```
   
 * Note the empty square bracket `[]` after first array bracket.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Multi-dimensional options array (for themes/plugins) with Settings API’
is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [setting api](https://wordpress.org/support/topic-tag/setting-api/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 3 participants
 * Last reply from: [Yudhistira Mauris](https://wordpress.org/support/users/maurisrx/)
 * Last activity: [11 years, 6 months ago](https://wordpress.org/support/topic/multi-dimensional-options-array-for-themesplugins-with-settings-api/#post-4552077)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
