views:

675

answers:

2

I am trying to create my first WordPress plugin. Even in trying to create the install function, things are being a pain. I want to set some global variables specific to my plugin rather than putting the literal values throughout the various functions. However, my install function does not pick up these global variables. Here is my code so far:

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

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

+1  A: 

It turns out if you want a global variable for your install function, you must declare it to be global.

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

More information can be found at the link below under the "A Note on Variable Scoping" section: http://codex.wordpress.org/Function%5FReference/register%5Factivation%5Fhook

Marshmellow1328
A: 

I also have same problem but luckily I found my fault. My biggest mistake is my sql query when I write like this

$sql = "CREATE TABLE". $tblAccounts." ( …

Should be having a space between TABLE and " it was very tricky to identify because I take two day facing this problem without notice the error… :(

Show and hide SQL error very helpful when debugging the error because it can tell you what happen at the back of the code.

One more thing to consider is register_activation_hook (file , function) function you must use it very carefully especially on file location you can put_FILE_ if your function is located at main file plugin if not you must put plugin_basename(l_FILE_ ) to activate it. You also can use add_action('activate_' . plugin_basename(l_FILE_), 'a2ez_install') as alternative.

I hope you can solve the problem. Good Luck.

cayrol