Support » Plugins » How to use register_activation_hook within class structure?

  • Hi,
    I’ve set my plugin up to operate with Objects / Classes.
    It works fine until I try to add the register_activation_hook() function. When I do this, it is causing my initial class to be redefined and I get an error on activation:

    Plugin could not be activated because it triggered a fatal error. Fatal error: Cannot redeclare class

    My digging around has led me to believe that the above function must reload my file somehow… i’m not sure.

    How do I use this properly if I’ve already defined my class. I’ve tried placing the function inside the class, outside the class, inside/outside isset() functions… Could someone point me in the correct direction? The code for my file is below. Again, if I were to comment out the register_activation_hook, it works correctly

    Class ClassName {
    	//this method sets the plugin up upon activation
    	function wpClassSetup(){
    if( class_exists('ClassName') ){
    	if( !isset($wpClassName) ) {
    		$wpClassName = new ClassName;
    if( isset($wpClassName) ){
    	//Register plugin
    	register_activation_hook(__FILE__, array(&$wpClassName,'wpClassSetup') );

Viewing 2 replies - 1 through 2 (of 2 total)
  • The following is what I generally do. Try it if you can.

    if( !class_exists( 'ClassName' ) ) {
      class ClassName {
        function ClassName() { }
        function wpClassSetup() { /* Stuff */ }
    if( class_exists( 'ClassName' ) ) {
      $cn = new ClassName;
      // Register for activation
      register_activation_hook( __FILE__, array( &$cn, 'wpClassSetup' );

    Thanks again… you’ve been a lot of help over the last week.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to use register_activation_hook within class structure?’ is closed to new replies.