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’).