Multiple arrays in one admin option from theme options page. (3 posts)

  1. cthomas88
    Posted 3 years ago #

    I am trying to create a multidimensional array in the wordpress database from the options pages. I have the form tag

    <form action="options.php" post="post">
    I get the options by

    <?php settings_fields( 'theme_styles' ); ?>
    <?php $options = get_option( 'theme_styles_options' ); ?>

    I have a field

    <input type="text" id="theme_styles_options[option_name]" name="theme_styles_options[option_name]" class="regular-text" value="<?php esc_attr_e( $options[option_name] ); ?> />

    I would like to be able to have this field multiple times but under different arrays e.g.

      'first_array' => array(
        'option_name' => 'value1'
      'second_array' => array(
        'option_name' => 'value2'

    I know at the moment it my current code saves as

      'option_name' => 'value1'

    How to I set my field to send the value to the options.php page to be saved as a multidimensional array? Also, if someone knows how to save it, how to I retrieve it from the field in the database?

    Many Thanks

  2. vtxyzzy
    Posted 3 years ago #

    This is only a partial answer, but you can store array(s) in the wp_options table by using the PHP function serialize().

    When you retrieve the option, you use unserialize to convert it back to the original array(s).

  3. Digital Raindrops
    Posted 3 years ago #

    This is a recent post, it has code for a nice theme setup page, it uses the option array, like in your example!

    There is a working twenty eleven child theme as a download, the template files have calls to the option array, so if you can follow the code it will give you just what you want!


    Look at the functions:

    Then there is some more in functions.php and the other files!

    In short it saves the values into an array and saves the array in a single option value.



Topic Closed

This topic has been closed to new replies.

About this Topic