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?

4 Answers 4


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
  • 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? Commented Feb 11, 2013 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
    Commented Feb 11, 2013 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. Commented Feb 11, 2013 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
    Commented Feb 12, 2013 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
    Commented May 21, 2017 at 11:43

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';

     * 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:

class nssUser_creator {
    static function nss_init() {
    static function nss_plug_install(){
        echo 'YOUR plugins is now activated ! YeaP';
}//end class


Based on code suggested by @Ivan Carrasco, I created this file:


Invoke it as such:

$match_score                = new Match_Score();
$plugin_activation_instance = PluginActivation::get_instance( 'compeer-matching-plugin.php', 'vinnies_connect_matching_cron', 'daily' );
$plugin_activation_instance->set_cron_callback( $match_score, 'calculate_all_scores' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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