WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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?";

// call the static method
share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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