Support » Plugins » 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)
  • 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:

    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

    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.

    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.




    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.


    You can test if a function exists before defining it:

    if (!function_exists('test_activated')) {
      function test_activated() {
        // your activation code


    you can try with
    instead of echo.

    This will throw
    “Plugin could not be activated because it triggered a fatal error.$debug

    maybe you can try with this
    register_activation_hook (plugin_basename(_FILE_), your function)

    @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…

    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.

    So, I had massive trouble with using register_activation_hook, too. I had done it exactly how it was described at 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!

    If you follow the instructions on the 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:
    and replace it with:
    and the code will work properly.

    NOTE: I’m using WP 3.0

    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:

    CATEGORY text NOT NULL default ”, // Tilhørsforhold
    NAME text NOT NULL default ”, // Navn
    TITLE text NOT NULL default ”, // Stilling

    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.