All global variables should be within a function or class

Hi,

I submit a WordPress theme in Themeforest and got a soft reject. One of the soft reject issues is:

All global variables should be within a function or class.

I wonder if global post, global $wp_query, global $paged it also need to be included in a function or a class to avoid a theme rejection?

Thanks

this is something new :slight_smile:

In my opinion it is very unwise. I think should not be

In my case i found, i consider functions.php (may be you are using some global variable there outside the function or class)

You can do something like this in functions.php

function my_global_var(){

   global $global_var;

   return $global_var;

}

And now in any file you can call it as

$my_global_var = my_global_var()

and further use it as

if('1'  === $my_global_var['property_name']) :

// do something

endif;

This looks pretty wild)

this is new requirement :slightly_smiling:
we can use a function to get global variables
But I don’t know why we need to do it :smiley:

1 Like

It’s not a mistake I do personally.

However, your reviewer might want you to do this for better organization and namespacing. You are importing the globals and not declaring them. In this case, you cannot control the name of the variable and now the variable $post or $paged is referencing the global variable post and paged accordingly.

Whereas if you used a function, you can prefix the function making it unique to your theme.

Note below, this is what the original poster is doing:
global $post, $paged;

What this does is imports the global $post into the variable $post at the file level. This variable is now available to the whole file and any other files included.

So a third party plugin, unaware of your usage of a variable $post or $paged within file scope could do :
$post = “bubba”;

You have now lost the original value of $post as the variable is not unique.

Your code tries to access $post and ends up with nothing but “bubba”. Bubba, who the hell is bubba?
Conflict :ok_hand:

So I think, but not your reviewer so just saying what makes sense to me and why I wouldn’t import globals at the file level :wink:

1 Like

Hi there

im struggling on the same,
guys do you have any working solution on this problem?
Yes i see there is a some different ideas and solution on the envato forums but does anyone has a final version?
:frowning:

Hi,

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

If a global variable was within a function or a class wouldn’t that make them not global? Maybe the rejection is because you used global variables. Object oriented purists do not like global variable and a variable defined within function is anything but global.

So it looks like they want you to define class variables and then pass them around. Which makes you very portable (even when portability is not an issue or needed) but also adds complexity (and thus reduces speed)

Seems a bit strange to require this unless they code is very generic and will be used as a module across many applications. Or they are requiring it to protect global variable namespace. For example, creating a global variable with a very generic name like x (for example) is quite dangerous so they police this by eliminating all global variables.