How to use register_activation_hook within class structure? (3 posts)

  1. Glenn Ansley
    Posted 7 years ago #

    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') );
  2. nickohrn
    Posted 7 years ago #

    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' );
  3. Glenn Ansley
    Posted 7 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic