Title: register_activation_hook DOES NOT WORK
Last modified: August 19, 2016

---

# register_activation_hook DOES NOT WORK

 *  [gale-andrews](https://wordpress.org/support/users/gale-andrews/)
 * (@gale-andrews)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/)
 * 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?

Viewing 12 replies - 1 through 12 (of 12 total)

 *  [gesman](https://wordpress.org/support/users/gesman/)
 * (@gesman)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217000)
 * 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](https://wordpress.org/support/users/jimrowe/)
 * (@jimrowe)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217013)
 * 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.
 *  [Brent](https://wordpress.org/support/users/brentes/)
 * (@brentes)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217016)
 * 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
 *  [Brent](https://wordpress.org/support/users/brentes/)
 * (@brentes)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217017)
 * 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
 *  [binlid](https://wordpress.org/support/users/binlid/)
 * (@binlid)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217026)
 * You can test if a function exists before defining it:
 *     ```
       <?php
       if (!function_exists('test_activated')) {
         function test_activated() {
           // your activation code
         }
       }
       ```
   
 * tom.
 *  [phpepe](https://wordpress.org/support/users/phpepe/)
 * (@phpepe)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217077)
 * you can try with
    `die($debug);` instead of echo.
 * This will throw
    “P_lugin could not be activated because it triggered a fatal
   error.$debug_”
 *  [cayrol272](https://wordpress.org/support/users/cayrol272/)
 * (@cayrol272)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217082)
 * maybe you can try with this
    register_activation_hook (plugin_basename(_FILE_),
   your function)
 *  [Franz Josef Kaiser](https://wordpress.org/support/users/f-j-kaiser/)
 * (@f-j-kaiser)
 * [16 years ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217087)
 * [@cayrol272](https://wordpress.org/support/users/cayrol272/): /root/wp-includes/
   plugin.php > line 548 … plugin_basename already defined in the function.
 * But: Has anyone any idea how to overwrite the plugin_basename function? I would
   like to use this in a theme. I can use any other settings API function, but not
   register_activation_hook…
 *  [mattifesto](https://wordpress.org/support/users/mattifesto/)
 * (@mattifesto)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217106)
 * OK, I figured out the “Fatal error: Cannot redeclare class” problem.
 * My activation hook function was a static method on the main plugin class:
 * `register_activation_hook(__FILE__, 'MyPluginClass::activate');`
 * In this function I included another file. In this other file, I used the $this
   variable mistakenly forgetting it was a static function so there was no $this.
   If I get the class instance as I should, for instance from a global, the error
   goes away.
 * In any case this is a bizarre and misleading error meaning there is an error 
   in the activation hook.
 *  [silviapfeiffer1](https://wordpress.org/support/users/silviapfeiffer1/)
 * (@silviapfeiffer1)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217108)
 * So, I had massive trouble with using register_activation_hook, too. I had done
   it exactly how it was described at [http://codex.wordpress.org/Function_Reference/register_activation_hook](http://codex.wordpress.org/Function_Reference/register_activation_hook)
   with register_activation_hook(__FILE__, ‘myplugin_activation_func’ ); However,
   it just was never called.
 * So, eventually I figured out that __FILE__ pointed to the actual location of 
   the file, which was not inside the wordpress plugin directly, where I had only
   linked the plugin directory. Since register_activation_hook links the function
   based on the filename, it wasn’t able to find it.
 * So, I eventually ended up doing this hack:
    register_activation_hook(WP_PLUGIN_DIR.‘/
   myplugin/myplugin.php’, ‘myplugin_activation_func’ );
 * Now it works, uff!
 *  [insightdesigns](https://wordpress.org/support/users/insightdesigns/)
 * (@insightdesigns)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217109)
 * If you follow the instructions on the [http://codex.wordpress.org/Creating_Tables_with_Plugins](http://codex.wordpress.org/Creating_Tables_with_Plugins)
   page, there are two issues with it:
 * 1) In the “The Whole Function” section, the first line of the code block needs
   to explicitly declare $jal_db_version as global. So the very first line of the
   block should be:
    `global $jal_db_version;`
 * 2) The dbDelta function causes a fatal error – the now infamous “cannot redeclare
   function xxxx” error that so many folks are having trouble with when trying to
   use this code. Remove this line:
    `dbDelta($sql);` and replace it with: `$wpdb-
   >query($sql);` and the code will work properly.
 * NOTE: I’m using WP 3.0
 *  [maxemil](https://wordpress.org/support/users/maxemil/)
 * (@maxemil)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217110)
 * Just a point here..
 * register_activation_hook will use the default message “cannot redeclare” on any
   error.
 * I tried to activate a plugin, where I had a table to be created.
    My problem 
   was that I had comments on my table rows… like this:
 *  `ID bigint(11) NOT NULL AUTO_INCREMENT,
    CATEGORY text NOT NULL default ”, //
   Tilhørsforhold NAME text NOT NULL default ”, // Navn TITLE text NOT NULL default”,//
   Stilling PRIMARY KEY ( ID )`
 * But the of course I realized you cannot do this, and when I removed the comments,
   the function worked 🙂

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘register_activation_hook DOES NOT WORK’ is closed to new replies.

 * 12 replies
 * 12 participants
 * Last reply from: [maxemil](https://wordpress.org/support/users/maxemil/)
 * Last activity: [15 years, 10 months ago](https://wordpress.org/support/topic/register_activation_hook-does-not-work/#post-1217110)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
