WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#6971 closed defect (bug) (invalid)

plugin variables not available as global in register_activation_hook function

Reported by: spikyjt Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.5.1
Component: General Keywords: register_activation_hook plugin global dev-feedback 2nd-opinion
Focuses: 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 (3)

comment:1 DD326 years ago

  • Keywords dev-feedback 2nd-opinion added

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.

comment:2 DD326 years ago

  • Milestone 2.5.2 deleted
  • Resolution set to invalid
  • Status changed from new to closed

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

comment:3 jacobsantos6 years ago

Plugin authors do some funny things sometimes.

Note: See TracTickets for help on using tickets.