WordPress.org

Ready to get started?Download WordPress

Forums

register_activation_hook / register_deactivation_hook (7 posts)

  1. dvg
    Member
    Posted 4 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.)

    Help?

  2. dvg
    Member
    Posted 4 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
    Member
    Posted 4 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
    Member
    Posted 4 years ago #

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

  5. nickCR
    Member
    Posted 3 years ago #

    http://wordpress.org/support/topic/312342?replies=2#post-1226615

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

  6. ooeygui
    Member
    Posted 3 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.php

    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(){ ... }
    
    }

    myplugin.php

    include_once("class.foo.php");
    $foo = new Foo(__FILE__);
  7. ooeygui
    Member
    Posted 3 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