Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I apologize if this is covered extensively elsewhere, I'm not sure how to phrase my searches on the topic.

Basically, instead of the following standard code to store data in WP-options...

$options = array (
array("name" => __('Font','mytheme'),
    "desc" => __('Change the font face)','mytheme'),
    "id" => "mytheme_font",
    "std" => "Arial",
    "type" => "text"),
array("name" => __('Alternate Font','mytheme'),
    "desc" => __('Change the alternate font face)','mytheme'),
    "id" => "mytheme_font_alternate",
    "std" => "Tahoma",
    "type" => "text"),
);

I would instead like to store multiple values in the "std" portion, with its own array - like this:

    $options = array (
        array("name" => __('Font','mytheme'),
            "desc" => __('Change the font face)','mytheme'),
            "id" => "mytheme_font",
            "std" => array('size' => '10px', 'face' => 'Arial', 'color' => '#000000'),
            "type" => "text"),
        array("name" => __('Alternate Font','mytheme'),
            "desc" => __('Change the alternate font face)','mytheme'),
            "id" => "mytheme_font_alternate",
            "std" => array('size' => '13px', 'face' => 'Tahoma', 'color' => '#FF0000'),
            "type" => "text"),
        );

I would assume this is allowed, but given that it is, I am at a loss for how to:

1) Know which name/id to use on the admin form inputs to signify to go into "size" and "face" and "color".

For example, in the standard code without the multidimensional array I would usually use code like this:

<input name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="text" value="<?php echo stripslashes(get_option( $value['id'], $value['std'] )); ?>" />

I'm not sure what to place in the "name" and "id" sections for the input.

2) How would I retrieve these values in the WP theme? I usually use code like this:

// Make values available
global $options;
foreach ($options as $value) {
$$value['id'] = get_option($value['id'], $value['std']);
}

// Print the actual value
<?php echo $mytheme_font; ?>;}

Any help is greatly appreciated! If anyone has any ideas or knows of any tutorials on this topic that would be excellent! Thank you!

share|improve this question

1 Answer 1

Assuming this array for example usage:

$options = array(
    "name" => __('Font','mytheme'),
    "desc" => __('Change the font face)','mytheme'),
    "id" => "mytheme_font",
    "std" => array('size' => '10px', 'face' => 'Arial', 'color' => '#000000'),
    "type" => "text",
 );

For question 1, to reference nested arrays, just reference them directly.

echo $options['std']['size'];

In the form input, if using the Settings API:

name="mytheme_options[std][size]"

Regarding question 2, it appears to me that you're probably doing it wrong to being with, as that bit of code you have there makes no real sense. What is get_option($value['id'], $value['std']); supposed to do anyway?

You'd get your options array from the database like this:

$options = get_option('mytheme_options');

Individual options would then be like $options['name'] or $options['std']['face'] or what have you.

share|improve this answer
    
Hi Otto, thanks for the help! I updated some of the code in my original question. If my $options array has many arrays within it, how would I access specific arrays within it? For example would I use an integer like this to access the 2nd array? $options[1]['std']['size'] I tried that but it didn't work. My starter code is from Sitepoint's "Build Your Own Wicked WordPress Themes" book. –  LearnWebCode Sep 9 '11 at 23:04
    
I just made a test with your code and referencing it by $options[1]['std']['size']; works fine for me. –  Otto Sep 10 '11 at 16:17
    
Hmm, maybe the part that is breaking it for me is the "mytheme_options" you used as the parameter for the "get_option" function. Where does "mytheme_options" come from and what is it? Is it an array I should have already created? Is it an array that WP automatically creates based on my themename and adds "options" to the end of its name? –  LearnWebCode Sep 13 '11 at 20:20
    
No, that's just a name I made up to designate where the options are saving. You're saving your own options, presumably you would know where they are, right? –  Otto Sep 14 '11 at 22:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.