• 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.

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

    I know at the moment it my current code saves as

    array(
      '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

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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).

    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!

    https://pastebin.com/P5h98ZUC

    Look at the functions:
    ‘dr_slider_theme_options_init()’
    ‘dr_slider_theme_options_add_page()’
    ‘dr_slider_options_render_page()’
    ‘dr_slider_theme_options_validate($input)’
    dr_slider_theme_admin_options()

    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.

    HTH

    David

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Multiple arrays in one admin option from theme options page.’ is closed to new replies.