Ticket #6971 (closed defect: invalid)

Opened 2 months ago

Last modified 5 days ago

plugin variables not available as global in register_activation_hook function

Reported by: spikyjt Assigned to: anonymous
Priority: normal Milestone:
Component: General Version: 2.5.1
Severity: normal Keywords: register_activation_hook plugin global dev-feedback 2nd-opinion
Cc:

Description

When the function called by register_activation_hook is requires variables that are declared in the main code of a plugin, they are not available as globals, however wordpress vars such as $wpdb are. For example:

$my_plugin_some_var = "foo";

register_activation_hook(__FILE__, 'my_install_func');

function my_install_func() {

global $wpdb; // wordpress var, this will be fine
global $my_plugin_some_var; // this will not work, the var will have to be re-declared here

....

}

This is particularly frustrating for plugins that create tables in the database and wish to store the table name in a variable for access in other functions that access data in those tables.

Change History

05/15/08 00:23:24 changed by DD32

  • keywords changed from register_activation_hook plugin global to register_activation_hook plugin global dev-feedback 2nd-opinion.

Unfortunately, Its not something that can be changed easily.

As plugins are included from inside a function(When activated), the variables' scope is that function, not global like it used to be under 2.3.x(i believe).

The solution is to use this code instead:

global $my_plugin_some_var;
$my_plugin_some_var = "foo";

register_activation_hook(__FILE__, 'my_install_func');

function my_install_func() {

global $wpdb; // wordpress var, this will be fine
global $my_plugin_some_var; // this will not work, the var will have to be re-declared here

....

}

See Also: #5860

However, Once activated, Plugins *area* included in the global scope, so the global keyword is no longer needed, but does no harm.

Suggest closing as wontfix unless theres a possible fix which i cant think of.

06/30/08 03:28:18 changed by DD32

  • status changed from new to closed.
  • resolution set to invalid.
  • milestone deleted.

Closing as Invalid, Plugins code needs to be changed to register variables as global.

06/30/08 04:51:18 changed by jacobsantos

Plugin authors do some funny things sometimes.