How to fix a WordPress theme soft reject Issues?

I submit a WordPress theme in themeforest and got a soft reject.
Soft reject issues is:-

  1. http://envato.d.pr/17HZf/5Vmkeiga
    All global variables should be within a function or class.

here $softhopper_pick; is Redux Global variable.
We know that to get redux value need to call redux Global variable. And sometimes in Wordpress post looping need to call $post etc global variable outside of a function or class. But themeforest reviewer said to me All global variables should be within a function or class.

I searched in google but I got no solution. If anyone know the solution please inform me.

Thanks

Global variables should be within function, then call the functions.

I got the exactly same issue as yours. Iā€™m wondering if there is a way of getting Redux values without a global variable.

I donā€™t know how Redux stores its options but I use get_theme_mod() to get values saved by options framework (SMOF), alternatively it can be get_option().
It can be something like this:
$value = get_theme_mod( 'option_name' ); if ( ! empty( $value ) { echo $value; // or do anything else }

@SoftHopper

As far as I know wordpress is using global variables (ex: wp_query) so it shouldnā€™t be any problem using global variablesā€¦ but

I think the reviewer is talking about this issue:

global $variable;
function MyFunctionThatIsUsingTheVariable(){}

I think you should rewrite something like this:

function MyFunction(){
  global $variable;
 //use the variable inside the function, not outside
}
1 Like

Hello Webdenim,
Thanks your help, Temporary I solve this problem like this:-
$softhopper_pick = get_option(ā€˜softhopper_pickā€™); // here softhopper_pick is the redux global option name;

But I donā€™t yet understand why reviewer soft reject this issue.

Thanks

Reviewer follow WordPress theme standard.

Wordpress theme standard isā€¦ Uhmmā€¦ How should I put it? Stupid as !@#$. Donā€™t use global, except inside functions or classes? Oh ok, fine, seems justifiable, but there is an inci tinci tiny problem. The footer.php file for example is included from WITHIN A FUNCTION called get_footer. Thus does it make any sense to you? Cause it sure doesnā€™t to meā€¦
Meeh fine, lets take another exampleā€¦
Hook me up babyā€¦ o.O
No really, use an overloaded action / filter system, so that you can hook up together and dance a bit and just break someone elseā€™s ( !@#$ ) code, while your at it. A classic do{ } while(); in terms of php.
God Iā€™m pissedā€¦ Do any of these reviewers actually have any idea about code optimization and PHP itself? I mean Iā€™m no expert on all of the matters, but some of the things that are required by WordPress to make a theme are just plain old stupid.
I didnā€™t mean to trash anyone, but god I had to get it out.
Regarding your original questionā€¦ I did it with a do action and made the logic inside the function.

Iā€™m a bit confused.

For example, Iā€™m using global variables to pass some information from a template ( single.php ) to a ā€œtemplate partā€ ( get_template_part() ). How would I go about putting that in a function?

Hey, so whatā€™s right way to write Redux global variable?

Itā€™s confused.

Regards,

Itā€™s a tongue in cheek solution but:
You could just create a silly singleton data class that holds your variable as a static property.

Then, as necessary, you can get and set that variable from pretty much anywhere.

I generally think this is a dumb use of OOP (using class names as namespces), but then and againā€“I donā€™t get to write the acceptance criteria.

For Redux, use get_option():
get_option( 'option_name', 'option_default_value_if_not_exists' )
Topic starter stated it here but later deleted post.

My theme have a lot of global variables, like $wp_query but If are inside function, will be approved?

Hi,

It also need a function or a class for global post, global $paged, global $wp_query to avoid a theme rejection?

Thanks

My solution is here:

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'];

Hi,

Do you have any suggestions on how I can get around this issue, thanks? Global variables within a function - Please Help

Hi,

Do you have any suggestions on how I can get around this issue, thanks? Global variables within a function - Please Help

Here is the final solution to avoid using globals for theme options. Redux.

if (!function_exists(ā€˜theme_get_fieldā€™)) {
function theme_get_field($var = ā€˜ā€™, $sub = ā€˜ā€™)
{
global $theme_field;
if ($sub) {
if (!empty($theme_field[$var][$sub])) {
return $theme_field[$var][$sub];
} else {
return ā€˜ā€™;
}
} else {
if (!empty($theme_field[$var])) {
return $theme_field[$var];
} else {
return ā€˜ā€™;
}
}
}
}

Than just use theme_get_field(ā€˜fieldā€™) or theme_get_field(ā€˜fieldā€™, ā€˜subfieldā€™).