Global Variables in WordPress Plugin

I am trying to create my first WordPress plugin. Even trying to create a setup function, everything gets hurt. I want to set some global variables specific to my plugin, instead of putting literal values ​​in all functions. However, my setup function does not pick up these global variables. Here is my code:

$version = '1.0a';
register_activation_hook( __FILE__, 'install' );
function install() {
  global $version;
  add_option( 'test_version', $version );
}

Obviously, this is pretty straight on my end. Any ideas what is going wrong here?

+3
source share
1 answer

It turns out that if you want to use a global variable for your installation function, you must declare it global.

global $version = '1.0a';
register_activation_hook( __FILE__, 'install' );
function install() {
  global $version;
  add_option( 'test_version', $version );
}

" ": http://codex.wordpress.org/Function_Reference/register_activation_hook

+7

Source: https://habr.com/ru/post/1720098/


All Articles