I'm trying to print a key from one select box. I'm using the WordPress Settings API. This is the array:

$options = array (
array(  "name" => "Styles & Fonts",
        "type" => "title"),
            array(  "type" => "open"),
            array(  "name" => "Color scheme",
                    "desc" => "Select the color scheme for the theme.",
                    "type" => "select",
                    "id" => "st_styles",
                    "options" => array("style", "dark", "grey", "green", "red"),
                    "std" => "style"),
            array(  "name" => "Loop style",
                    "desc" => "Select the style loop for the theme.",
                    "type" => "select",
                    "id" => "st_loops",
                    "options" => array('default' => 'Normal (default)', 'small' => 'No thumbnails', 'large' => 'Big thumbnails'),
                    "std" => "style"),
            array(  "type" => "close"),

You can see 2 examples on this code, the first has the options in an array, and the second has the options in an associative array.

I can get the value with this code:

echo get_option('st_styles', 'style')

If the selected option is 'dark', it prints 'dark'. By default it's 'style'. This works fine!!

But in the second example I used an associative array because I want to show 'Normal (default)' in the select box; then I want to use 'default' (without spaces) as a CSS class. I tried:

echo get_option('st_loops', 'default')[key];
//It returns me the letter 'N'; the first of 'Normal (default)', I think.

print array_keys(get_option('st_loops', 'default'));

echo get_option(array_keys('st_loops', 'default'));

And finally I tried with this:

global $options;foreach ($options as $value) {
    if (get_option( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_option( $value['id'] ); }

    echo $st_loops;
        //Prints 'Normal (default)'.
    echo $st_loops[key];
        //Prints 'N'.
    echo $st_loops[$key];
        //Prints 'N' again.
    print array_keys($st_loops);
        //Prints nothing.
    echo key($st_loops);
        //Prints nothing.

So I have no idea what to do about this. I'm sure it's easy but I'm being unlucky.

Updated 1: This:

global $options;foreach ($options as $value) { 
if (get_option( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_option( $value['id'] ); }

returns this:

[29] => Array
        [name] => Loop style
        [desc] => Select the style loop for the theme.
        [type] => select
        [id] => st_loops
        [options] => Array
                [default] => Normal (default)
                [small] => No thumbnails
                [large] => Big thumbnails

        [std] => style

Updated 3: At the moment, this is the code as provisional solution:

$loop_class = get_option('st_loops');
if($loop_class == "Normal (default)") { $loop_class = "default"; }
elseif($loop_class == "No thumbnails") { $loop_class = "small"; }
elseif($loop_class == "Big thumbnails") { $loop_class = "large"; }
echo $loop_class;

Easiest way, but too many lines, I think.

  • This is really just PHP. var_dump(get_option('st_loops', 'default')); should tell you all you need to know, and if something is wrong it should help you spot the problem. – s_ha_dum Aug 29 '13 at 14:35
  • Thank you. It returns this: string(16) "Normal (default)". Is 'string(16)' a reference to use somewhere? :) – Gerard Aug 29 '13 at 14:41
  • Post all of the relevant code please. – s_ha_dum Aug 29 '13 at 14:46
  • This was the full value returned. Anyway I did a print_r() and I updated the post with the information :) – Gerard Aug 29 '13 at 17:25
  • That is the value, yes, but I am asking for the code-- something I van copy an paste into a dev stack and test. – s_ha_dum Aug 29 '13 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.