WordPress 2.6: register_activation_hook Cannot See Global Variables


September 3rd, 2008

I have recently been trying to update an old plugin I created for WordPress 2.3.2 so that it would be compatible with the latest version : 2.6.

I am currently having an issue where the register_activation_hook function (and this may not be limited to this one function) where it refuses to reference the global variables that I define in the plugin.

Take the below as an example (proper code attached at the bottom):

require 'mcn_constants.php';

$mcn_test_blabla    =   "Well ??????";

register_activation_hook(__FILE__,'mcn_test_install');
register_deactivation_hook(__FILE__, 'mcn_test_uninstall' ) ;

add_action('parse_query', 'mcn_test_check');

function mcn_test_install() {
    global $this_is_broke, $mcn_test_blabla;

    add_option("mcn_test_value", "The value is: $this_is_broke And :: $mcn_test_blabla ");
}

function mcn_test_uninstall() {
    delete_option("mcn_test_value");
}

function mcn_test_check() {
    global $this_is_broke;

    echo get_option("mcn_test_value");
    echo "Proper value : $this_is_broke";
}

When you load the front page of your blog with the above plugin code activated you should see :

The value is: YES I AM BROKE!!!!!
And ::Well ??????

Proper value : YES I AM BROKE!!!!!

… but instead you see:

The value is:
And ::

Proper value : YES I AM BROKE!!!!!

As you can see – the $this_is_broke and $mcn_test_blabla variables are not being set in the Option “mcn_test_value”.

Yet calling the global variable in the function “mcn_test_check”, which is called as part of the “parse query” action, will show the global variable – as it should.

So – is this a bug or am I missing something fundamentally different in WordPress 2.6? Using global variables in WordPress 2.3.2 worked just fine.

I have attached a sample plugin that will replicate this issue.

Any ideas?

mcn_test.rar

Tags: , , , ,

Categories: ALM, Programming, Wordpress

"Thought leadership is how winners are differentiated in business."