#6971 closed defect (bug) (invalid)
plugin variables not available as global in register_activation_hook function
| Reported by: | spikyjt | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | General | Version: | 2.5.1 |
| Severity: | normal | Keywords: | register_activation_hook plugin global dev-feedback 2nd-opinion |
| Cc: | Focuses: |
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.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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.