3

I'm currently trying to make my own theme, but i'm not quite sure how the get_option() function works, because how come this..

echo get_option('show_header', 'sultenhest_theme_display_options');
echo get_option('sultenhest_theme_display_options')['show_header'];

..both returns 1 (but Dreamweaver doesnt like the second option). While

echo get_option('twitter', 'sultenhest_theme_social_options');

..simply returns 'sultenhest_theme_social_options', which is incorrect.

An option would be to define the array as such

$social_options = get_option( 'sultenhest_theme_social_options' );

and call it like this

echo $social_options['twitter'];

It returns the correct string, but it only works in the header.php (if the array is defined there) file and not in, e.g. footer.php.


UPDATE: Following partly ialocin and Brad Dalton i came up with this solution, which works like a charm:

function sultenhest($option, $arg){
  $the_array = array();
  foreach( get_option('sultenhest_theme_'.$option) as $key => $item ){
    $the_array[$key] = $item;
  }
  return $the_array[$arg];
}

and echoing it out like this:

echo sultenhest('social_options', 'twitter') ? '<a href="' . sultenhest('social_options', 'twitter') . '">Twitter</a>' : '';
4

You are using get_option() wrong, first variant, so take another look at the get_option() documentation. Basically you can't directly access an array element with the function, it just doesn't support it.

The second variant should be possible, but you need at least PHP 5.4 - if I'm not totally mistaken; I don't know anything about dreamweaver.

The reason why you can't access the variable everywhere you want is the variable scope, read up on it at the PHP Manual: Variable scope.

The easiest thing I imagine is to do it the old fashion way, just make a function you can use. Exemplary like this:

function wpse_179693_echo_my_options_array_value() {
    $social_options = get_option( 'sultenhest_theme_social_options' );
    if ( isset( $social_options['twitter'] ) ) {
        echo $social_options['twitter'];
    } else {
        return FALSE;
    }
}
  • 1
    get_option('some_option')['some_key'] actually does work in php 5.4, it's called array dereferencing. – Milo Feb 27 '15 at 23:00

Your Answer

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

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