Search code examples
phpwordpresshtml-input

Store an array through the option page with text box


I have done a option page in Wordpress. i have one text box and put one vale in one id. Now i want to store an array of values in one ID. My code for store one text in theme option.

array(  "name" => __('Text'),
                    "desc" => __('Your Text username, to be used on the links'),
                    "id" => $shortname."_test",
                    "std" => "Tset",
                    "type" => "text"),

To save the data from text box

case 'text':
        ?>
        <tr valign="top"> 
            <th scope="row"><label for="<?php echo $value['id']; ?>"><?php echo __($value['name'],'twentyeleven'); ?></label></th>
            <td>
                <input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_option( $value['id'] ) != "") { echo get_option( $value['id'] ); } else { echo $value['std']; } ?>" />
                <?php echo __($value['desc'],'twentyeleven'); ?>

            </td>
        </tr>

I want to create more text field and save in one ID.


Solution

  • Serialize the array, or Json Encode it.

    That will effectively store the array into your options table under its appropriate ID as a string. When you pull the option, you can Unserialize or Json Decode the option back into an array.

    Json Encode/Decode is usually preferred, but bear in mind that when decoding it, it defaults to type Object. Set the second parameter in your call to Json Decode to true so that it's returned as an associative array.