Gale Andrews
Member
Posted 2 months ago #
I did a search here for register_activation_hook and saw a lot of people asked questions about why it was not working and there aren't any replies to most of those threads.
Why doesn't this function do anything? Was it replaced with another function in the last upgrade or something?
gesman
Member
Posted 2 months ago #
After long struggle with it I found why.
1. register_activation_hook() must be called from the main plugin file - the one that has "Plugin Name: ..." directive.
2. Your hook function must be in the same file as well. From that function it is ok to call other functions defined in other files.
3. Doing echo "My hook called!"; - does not work from it. So this is not a good way to judge whether it working or not.
4. Your global variables must be explicitly declared as global for them to be accessed from inside of my_activation_hook().
Here's a tiny plugin code sample with working code to demonstrate all that:
<?php
/*
Plugin Name: A Test
Description: A Test
*/
require_once (dirname(__FILE__) . '/my_other_file.php');
// This code *will not* work. Activation hook function must be defined in the main plugin file.
// register_activation_hook (dirname(__FILE__) . '/my_other_file.php', 'my_other_function');
// This code will work.
register_activation_hook (__FILE__, 'test_activated');
// This is correct way to declare/access globals.
global $some_var; // globals must be declared explicitly. Without this you will not be able to access '$some_var' from within 'test_activated()' function.
$some_var = 'hey';
//===========================================================================
function test_activated ()
{
global $some_var; // Now it will be 'hey'.
// This function is defined in 'my_other_file.php'
my_other_function ();
// This will not work, so don't try. If you need logging write something in temporary log file in here via fopen/fwrite.
// If you want to quickly test if your activation hook works - put exit() into it. At least you'll see error during activation.
echo 'test_activated called!';
}
//===========================================================================
?>
Gleb Esman
jimrowe
Member
Posted 1 month ago #
I have a problem with this function causing all of my other functions in the main plugin file to be seen as declared thus giving "Fatal error: Cannot redeclare".
I am guessing that by saying __FILE__ it loads the file and suddenly it's been loaded twice.
Any ideas!? I have searched the forums but everyone seems to talk about global scope which I don't see has anything to do with it.
brentes
Member
Posted 1 month ago #
Hi jimrowe,
I'm having an identical problem. I haven't found a solution yet. Have you?
If I find one I'll let you know.
Cheers,
Brent
brentes
Member
Posted 1 month ago #
jimrowe,
I've sinced determined it's not the hook or my code causing the error.
I hadn't properly deactivated the plugin at some stage during development. I'm working with WPMU so to resolve the error I deleted the name of the plugin in the sitemeta db table under the meta_key "wpmu_sitewide_plugins".
Check your wp_sitemeta and/or wp_options tables to make sure the plugin isn't still being considered active. An easy way to test if this is the problem is to try your plugin on a clean WP install.
Hope that helps.
Brent
You can test if a function exists before defining it:
<?php
if (!function_exists('test_activated')) {
function test_activated() {
// your activation code
}
}
tom.