0

I tried to develop a plugin in OOP approach. I tried to load all the function needed inside the constructor class e.g action hooks. The problem is this hook "register_activation_hook". It wont load when the plugin is being activated. My main plugin file is in the root directory of my plugin and my class files are inside "classes" folder.

I read the usage of this function on this link http://codex.wordpress.org/Function_Reference/register_activation_hook and it says that the $file parameter is:

$file (string) (required) Path to the main plugin file inside the wp-content/plugins directory. A full path will work.

So I tried to use it like this inside my contructor class.

register_activation_hook(__DIR__ . '/../yslider.php',array($this, 'yslider_install'));//Installing data 

But still not working. Well it work if I put it on the main plugin file. But I think couldn't be an OOP approach?

Can anyone tell me what is the right way in using this function in oop appraoch?

3

It doesn't really matter. If you simply must have it inside a class, I would use a constant and a static method.

// in the main plugin file
define( 'MYPLUGIN_FILE', __FILE__ );

// include another file with this class in
class MyPlugin {

    public static function init() {
        register_activation_hook( MYPLUGIN_FILE, array( 'MyPlugin', 'install' ));
    }

    public static function install() {
         echo "little lamb, who made thee? is it wordpress or is it me";
    }
}

// call the static method
MyPlugin::init();
  • Still not working. I included the class with the static method. But I cant use that constant variable because its in different file, it is on the root directory. Is that mean that I need to include my main plugin file inside my class file? – Yves Gonzaga Feb 11 '13 at 17:42
  • @YvesGonzaga you can use constants from different files. you need to include the class file from your main plugin - that's the only way it will load anyway – djb Feb 11 '13 at 17:46
  • Ahh, WOW I didn't know that constant variable in PHP works that way. I am really thankful for your time helping me. Have you tried implementing a plugin using that approach? Well, in my case still that solution didn't work out. Maybe register_activation_hook really wont work if its not on the main plugin file. I will just consider putting this on my main plugin file and have a procedural approach for now. Thanks so much again. – Yves Gonzaga Feb 11 '13 at 17:53
  • i have tried it and it does work. substitute the 'echo' for a call to die('xyz') in the install() function and you'll see it happen when you hit activate. – djb Feb 12 '13 at 10:33
  • @JimMaguire, sorry! Good point. I hadn't tested it, and the echo was just for demonstration purposes. Probably better to die() or something, as you point out. Please feel free to make a multi-character edit :). – djb May 21 '17 at 11:43
1
//define
define('DIR_PATH',plugin_dir_path(__FILE__));
//class
class nssUser_creator {
    //method
    static function nss_init() {
        register_activation_hook(DIR_PATH,array('nssUser_creator','nss_plug_install'));
    }
    static function nss_plug_install(){
        echo 'YOUR plugins is now activated ! YeaP';
    }
}//end class

//object
nssUser_creator::nss_init();
1

Also in a a more OOP approach, you could do it in the following way:

In your plugin class :

class MyPlugin {

    private $file_base;

    /* 
     * Useful in case you put a lot of stuff in this class that
     * could be used later from other classes without knowing if the
     * class has already been instantiated. If it's just for the 
     * initialization without further using, go with a simple 
     * constructor
     */
    public static function get_instance(){
         static $plugin;
         if ( !isset( $plugin ) ){
             $plugin = new MyPlugin();
         }
         return $plugin;
    }  

    /*
     * If you go with a singleton, make it private, public otherwise
     */
    private function __construct() {
         $this->file_base = plugin_dir_path( dirname( __FILE__ ) ) . 'your_plugin_main_file.php';
         $this->init();
    }

    /*
     * Should be invoked just from the constructor, so private
     */
    private function init() {
         register_activation_hook( $this->file_base, array( $this, 'install' ) );

    }

    /*
     * This is the callback, so make it public
     */
    public function install() {
         echo "little lamb, who made thee?";
    }
}

and then in your plugin main file (eg: your_plugin_main_file.php), just:

MyPlugin::get_instance();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.