#6971 closed defect (bug) (invalid)
plugin variables not available as global in register_activation_hook function
Reported by: |
|
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)
Note: See
TracTickets for help on using
tickets.
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:
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.