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

Problem while retrieving options #103

Closed reardestani closed 11 years ago

reardestani commented 11 years ago

I have implemented the latest version of framework with defaults options, but I can not retrieve options.

I have tried two methods:

  1. <?php echo $options['text_demo']; ?> It returns a notice error, undefined text_demo .
  2. <?php $options = get_option(Redux_TEXT_DOMAIN); echo $options['text_demo']; ?> It returns null and display nothing at all.

Any solutions?

evertiro commented 11 years ago

Well... that'll definitely cause errors. The first method you're not actually getting the options from the database, so that's not going to work. Second method, you're trying to get the options from the database, but you're referencing the wrong thing. Redux_TEXT_DOMAIN is just that... a textdomain. Or, more accurately, a named constant representing your textdomain. I suggest taking a look at this if your are unfamiliar with that concept. What you should be doing is something like this...

$options = get_option('optname');
echo $options['my_option'];

where optname is whatever you have $args['opt_name'] set to in options.php and _myoption is the ID of an option.

reardestani commented 11 years ago

Thanks for your response,

I did the following steps:

  1. I find Redux_TEXT_DOMAIN in options.php and default.php, then replace it with Redux_example.
  2. I find $args['opt_name'] and set the value to example.
  3. $options = get_option('example'); echo $options['text_demo'];

But, I get error in retrieving options. Where am I doing wrong?

evertiro commented 11 years ago

For starters, you're misunderstanding what the textdomain does. It is a defined constant and should not be changed globally. All you need to do with it is change the initial definition in options.php if you want. As for your options issue... Does an option called example exist in the sections array?

reardestani commented 11 years ago

I think I found my mistake. There is no need to change text_domain. The only thing that I have to change is value of $args['opt_name'] in options.php. I did the following and it works: $args['opt_name'] = "example";

To retrieve : $options = get_option('example'); echo $options['text_demo'];

Is it possible to put $options = get_option('example'); in a file like options.php and from then on use only echo $options['text_demo'];.? I do not want to rewrite $options = get_option('example'); whenever I retrieve an option.

evertiro commented 11 years ago

In your functions.php (or another file that is called at the beginning of the init process), add $options = get_option('example') like you normally would, but make $options a global. In other words...

global $options;

$options = get_option('example');

Then, you won't have to redefine $options everywhere.

reardestani commented 11 years ago

I have done the following methods, but no resault.

To Test all the following methods, I added them at the top of the functions.php.

global $options;  $options = get_option('shymarkets');
function redux_options(){
  global $options; 
  $options = get_option('shymarkets');
}
add_action('wp_head', 'redux_options'); 
function redux_options(){
  global $options; 
  $options = get_option('shymarkets');
}
add_action('init, 'redux_options'); 

Am I doing sth wrong again?

reardestani commented 11 years ago

Any solutions?

evertiro commented 11 years ago

You're not really showing me enough to judge... creating a global should work fine, so knowing how and where you're trying to use it would be really helpful.

reardestani commented 11 years ago

Ok. the following codes are first lines of my theme functions,php. <?php

/* Bootstrap the Theme Options Framework */ if ( file_exists( get_template_directory() . '/admin/options.php' ) ) require_once('admin/options.php');

function redux_options(){ global $options; $options = get_option('example'); } add_action('wp_head', 'redux_options')

tridungpham commented 11 years ago

if you use

$options = get_option('example');

then in setup_framework_options() function, you have to set

$args['opt_name'] = 'example';

just make sure there are redux options that were stored in your database. You can check WP_Options table:

SELECT * FROM WP_Options WHERE option_name LIKE 'example' --the opt_name value 

sorry for my bad english

reardestani commented 11 years ago

My problem is not the thing that you mentioned. My issue: I want to call

global $options; $options = get_option('example');
once, then I can easily retrieve may options
echo $options['text_demo'];
without redefining
global $options; $options = get_option('example');
.

I hope it make sense.

kaymmm commented 11 years ago

if you copy your functions.php and options.php into http://pastebin.com/ it will be easier to see what you are trying to do and where your problems might be.

reardestani commented 11 years ago

Ok.

keoshi commented 11 years ago

Any luck with this? I too am having problems trying to define the options var globally, it only works when called right before the actual echo $options['text_demo'];

reardestani commented 11 years ago

I could not solve it, so I am now using Default Wordpress Live Customizer. It is really great. Try to give it a try.

keoshi commented 11 years ago

@ghost1227 could you weigh-in here, please?

angrycodes commented 11 years ago

if anyone is still interested, I wrote simple function that does what @reardestani asked for:

put this to functions.php

 function my_option($selected_option) {

    $options = get_option('example');

    return $options[$selected_option];

}

now, wherever you want to pull an option in your theme just write:

echo my_option('text_demo');

instead of writing

echo $options['text_demo'];

hope that helps