CMS Mastering Development Wordpress

Global variables and re-use

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.

Leave a Reply

Your email address will not be published. Required fields are marked *