1

Currently I'm writing a WordPress plugin. I would try to re-use variables from a function on different pages. Let's say I would create a function like this:

function test() {
   global $hello;
   $hello = 'hello world';
}

add_action( 'wp_head', 'test' );

I can do now on other pages: echo $hello; This is useful for me because I can get information like course settings from my plugin with $course_settings, without rewriting the whole code.

I am not sure of this is the right way to do? Because other plugins could rewrite my variables.

1
  • You should avoid using global variables wherever possible. If you want to get your plugin's settings, then just return them from a function, or just use get_option() if they're already saved as one option. Jun 30, 2020 at 11:33

2 Answers 2

0

In general programming, globals are bad practice:

  • You can't easily look at the use of a global in one place and see all the other uses, e.g. where it was set or will be read later.
  • You have many places to change the use of that variable if e.g. the use or format of it changes.
  • This makes it hard to make reliable changes to your code and to debug where/who set a global.
  • Other code could accidentally write over your variables.

However, Wordpress is a huge piece of PHP code, and if you want to write code which accesses the same information in many different places all over Wordpress, globals are a way to achieve this without writing complicated data structures, and plugins do use them to achieve this as other solutions can be much more complicated or unncessary

There are a few things you can do to make your use of globals as safe and bug-free as possible, here are a few suggestions:

  • Give your global a very unique and descriptive name. E.g. $mozboz_highscoreplugin_scores . This prevents possible conflicts where other code might accidetally overwrite your gglobal

  • Document the use of the global somewhere! E.g. at the top of your plugin describe what globals it uses and how

  • If you are a more advanced PHP programmer, you may want to try doing more clever things such as put all your variables inside a single global with an array, or even inside a class where you have much more control over the data and it may be easier to debug. For example:

    • Instead of:

      global $my_plugin_variable1;
      global $my_plugin_variable2;
      $foo = $my_plugin_variable1 + $my_plugin_variable2;
      
    • Try:

      global $my_plugin_datastore;
      $foo = $my_plugin_datastore['var1'] + my_plugin_datastore['var2']
      
  • Uses of classes and e.g. singletons is more advanced but may give you some benefit because it allows you wrap up and pass around all the functions as well as the variables in your plugin's program. E.g. in the example given perhaps you want to be able to call test() from anywhere, and in this case it might be nice to be able to do this from anywhere:

    $myplugin_object = getPluginInstance();
    $foo = $myplugin_object->test();
    
0
-1

Well if u already use your variable in the global scope, you should also define it in the global scope, not inside the function.

You can then simply include the php file where u define ur global variable (lets call it source script) into the script u wanna use, with a simple include or require statement before using it. I precisely work like this too in my plugin. However, make sure u prefix such globally used custom variables, by defining them like so:

$your_plugin_name_variable

To avoid naming conflicts with other wp-globals or other globals coming from other plugins.

If your global variable has a constant content, you could also define it inside a class as public static variable. This is especially recommended if you would like to load several global variables / functions on different pages. They would then all be safely stored under the class name, and you don't need to worry about naming collisions for all of them (just make sure ur class name is unique though). You can then simply use them in your script without the need of instantiating a class object (that's why u may define them as public static).

If your global has a particular dynamic content for which the first method I explained is not suitable, consider storing it in a database in key value style. Then your source script would simply retrieve its value from the database, and you would then relate the retrieved value to your global variable's name, and include that source script wherever u use it, as in proposition 1.

I recommend u to use one of these solutions, I actually use all of them with my plugins in function of the respective complexity.

1
  • Please provide a reasonable reason for downvoting this, there's absolutely no risk in doing as above if u take all the precautions and coding standards. Proof are all of my working websites, without any problem ever....
    – DevelJoe
    Jun 30, 2020 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.