Redux framework global variable issue

wordpress

#1

Hello guys. I need your help. I’m used Redux Framework Option panel on my theme. But Envato Reviewers soft rejected my item. I’m sorry. I’m not good php developer.

Rejection issue is: Globals should always be within a function or a class and should be used restrictively & only if theme really needs to. It’s highly recommended not to use them at all just to keep things out of the global namespace, they’re poor coding practice. http://envato.d.pr/1165K/4ZmfhB7P

I’ve changed it on function. but it not working. How can i fix it.

function my_global_var(){
global $global_var;
return $global_var;
}
my_global_var();
echo $global_var[‘opt_name’];

Please suggest me solution. thanks.


#2

may be

function my_global_var($val){
global $global_var;
return $global_var[$val];
}

echo my_global_var(‘opt_name’);


#3

Thanks for the suggestion.
I found solution for this issue.

if ( !function_exists('my_global_var') ) {
    function my_global_var($sm_opt_1, $sm_opt_2, $sm_opt_check ){
        global $opt_name;
        if( $sm_opt_check ) {
            if(isset($opt_name[$sm_opt_1][$sm_opt_2])) {
                return $opt_name[$sm_opt_1][$sm_opt_2];
            }
        } else {
            if(isset($opt_name[$sm_opt_1])) {
                return $opt_name[$sm_opt_1];
            }
        }
    }
}

Example:

my_global_var('opt_val','',false); // old redux code $opt_name['opt_val'];
my_global_var('opt_val','opt_val_2',true); // old redux code $opt_name['opt_val']['opt_val_2'];

#4

You can also use get_option:

$my_global_var = get_option( 'my_global_var' );
$my_setting = empty( $my_global_var['my_setting'] ) ? '' : $my_global_var['my_setting'];
echo $my_setting;

The only problem is that it won’t update the option when using the customizer.


#5

Hi @geniuspro,

Did you get your theme approved with this solution?

Thanks in advance for your help.


#6

Hello @pearlthemes
Reviewers not rejected again with this issue.


#7

If anyone have final solution please share with us.


#8

Hi @codetracks,

@geniuspro told that he didn’t get rejected his theme due to this issue after the solution he shared.

So we may use his solution.

Thank You.