evertiro / historical-redux2

A simple, easily extendable options framework for WordPress based on NHP Theme Options Framework.
http://reduxframework.com
Other
105 stars 43 forks source link

Checkbox/Switcher option problem #115

Closed marcinszpanowski closed 11 years ago

marcinszpanowski commented 11 years ago

Hi, I'm the very beginner in php so I decided to post for a solution.

I've made a checkbox option:

array( 'id' => 'motto', 'type' => 'checkbox', 'title' => ('Motto Option', Redux_TEXT_DOMAIN), 'desc' => ('Motto option.', Redux_TEXT_DOMAIN), 'switch' => false, 'std' => '1' // 1 = checked | 0 = unchecked ),

And I have problem with implementation to index.php

I've tried:

<?php

if(isset($options['motto']) && $options['motto'] == '1') { echo "Motto appears here"; } else { echo "You don't want motto."; }

?>

In both cases (if checkbox is checked and unchecked) it gives me else echo. Anybody could help with it ? What am i doing wrong ?

Thanks in advance.

evertiro commented 11 years ago

First thought... are you actually getting the options array? You can't simply reference $options['motto'] from anywhere unless the system already has the array stored to $options.

marcinszpanowski commented 11 years ago

I put <?php $options = get_option('mythemename'); ?> in header.php, right after doctype.

Is it enough ?

evertiro commented 11 years ago

No. There's two problems with that... one, something like that probably doesn't belong in the header. It should be early in the code, but the header is for including stylesheets and scripts, printing meta information and the like... not for setting variables. Second, defined that way, $options is a local variable. What this means is that it will not be accessible outside of the function that defined it. My recommendation is this...

  1. Move your variable declaration to functions.php.
  2. Make your variable global as such:
global $options = get_option('mythemename');
marcinszpanowski commented 11 years ago

Work like a charm!

I added this code:

global $options; $options = get_option('example');

Thank you for your support!

evertiro commented 11 years ago

Any time! Glad I could help!