register_activation_hook / register_deactivation_hook (7 posts)

  1. dvg
    Posted 6 years ago #

    So ... I just don't understand what's happening here.

    I'm written a class for my plugin, and register_deactivation_hook works, but register_activation_hook doesn't.

    class Foo{
       function Foo(){
          register_activation_hook( __FILE__, array( &this, "initializeFoo" );
          register_deactivation_hook( __FILE__, array( &this, "killFoo" );
       function initializeFoo(){
          add_option( "foo_start", 1 );
       function killFoo(){
          add_option( "foo_die", 1 );
    add_action( 'plugins_loaded', 'loadThePlugin' );
    function loadThePlugin(){
    	global $Foo;
    	$Foo = new Foo();

    When I activate the plugin, register_activation_hook appears not to fire, but when I deactivate it, register_deactivation_hook does. (The option appears in the database for foo_die, but not foo_start.)


  2. dvg
    Posted 6 years ago #

    Typo: I correctly have a $ in front of the this object.

    &$this is how I have it in the plugin; still doesn't work

  3. Gale Andrews
    Posted 6 years ago #

    Mine either. I wish someone could answer these questions about register_activation_hook so this wont be such a big issue.

    [you have made your point - now stop spamming numerous threads with it - samboll]

  4. nkuttler
    Posted 6 years ago #

    activation hook is fired before plugins_loaded... code is poetry...

  5. nickCR
    Posted 5 years ago #


    Check that out guys, was having the same problem. registration must be done from main file.

  6. ooeygui
    Posted 5 years ago #

    Actually... I got this exact thing to work outside of the main file. register_activation_hook() doesn't necessarily need to be called from the main plugin file but the first parameter($file) needs to point over to the main file.

    Try something like this:


    class Foo {
        public function Foo($file){
            register_activation_hook($file, array(&$this, "activate"));
            register_deactivation_hook($file, array(&$this, "deactivate"));
        public function activate(){ ... }
        public function deactivate(){ ... }


    $foo = new Foo(__FILE__);
  7. ooeygui
    Posted 5 years ago #

    If you reverse engineer the register_activation_hook() function you'll see that it all goes right back to the add_action("activate_(plugin file name)", "my_function"); method of doing it.

    register_activation_hook() simply strips the $file parameter down to be used in the old activate action.

Topic Closed

This topic has been closed to new replies.

About this Topic