[wp-trac] Re: [WordPress Trac] #6971: plugin variables not available as global in register_activation_hook function

WordPress Trac wp-trac at lists.automattic.com
Thu May 15 00:23:24 GMT 2008


#6971: plugin variables not available as global in register_activation_hook
function
-----------------------------------------------------------------------------+
 Reporter:  spikyjt                                                          |        Owner:  anonymous
     Type:  defect                                                           |       Status:  new      
 Priority:  normal                                                           |    Milestone:  2.5.2    
Component:  General                                                          |      Version:  2.5.1    
 Severity:  normal                                                           |   Resolution:           
 Keywords:  register_activation_hook plugin global dev-feedback 2nd-opinion  |  
-----------------------------------------------------------------------------+
Changes (by DD32):

  * keywords:  register_activation_hook plugin global =>
               register_activation_hook plugin global dev-
               feedback 2nd-opinion

Comment:

 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.

-- 
Ticket URL: <http://trac.wordpress.org/ticket/6971#comment:1>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list