tags:

views:

415

answers:

1

Hi,

I'm writing my first wordpress plugin and I'm trying to create a function to be called when the plugin is activated.

Currently it looks like this:

class ThumbsUp {

...

}

global $thumbs;
function thumbs_install() {
    //global $thumbs;
    $thumbs = new ThumbsUp();    /* Line 160 */
    $thumbs->installThumbsUp();
}  /* Line 162 */

// When plugin is activated -> install.
register_activation_hook(__FILE__,'thumbs_install');

But when I activate the plugin I get the following error:

Plugin could not be activated because it triggered a fatal error.

Fatal error: Cannot redeclare thumbs_install() (previously declared in /dev/site/wp-content/plugins/thumbs-up/thumbs-up.php:160) in /dev/site/wp-content/plugins/thumbs-up/thumbs-up.php on line 162

I've googled and looked and it's talked about as a variable scope issue but I can't find any examples of the answer and my php is not strong enough to translate the discussion into code.

Here's the solution described:

Any global variables that you want to reference inside the function
that is called by register_activation_hook() must be explicitly
declared as global inside the main body of the plugin (ie. _outside_
of this function). The plugin file is include()-ed inside another
function at the point where it is activated unlike at others times
when the plugin file is simply include()-ed. Phew. Bit of an odd one
to get your head around but there we go.

I thought I had done what is described but I still get the error. I've also tried every other combination of where I could possibly put the global $thumbs..

Any ideas?

Thanks

+2  A: 

There is a more generic answer to this question: every error that occurs in the code that is run from the function registered with register_activation_hook will be shown as "cannot redeclare ... " instead of the actual error. I suspect this is because of the way WordPress includes the plugin file when it calls the activation hook.

Jarkko Laine