0

I'm testing the plugin activation detection because something went wrong in my plugin. I discovered the problem, but didn't understand it:

I'm trying to check for plugins activation (others and my own) but inside an WordPress hook (init, for instance)

In the first example i will listen to the "Hello Dolly" plugin activation:

add_action('init', function(){
    register_activation_hook('hello.php', function(){
        echo 'Hello was activated.';
    });
    do_action('wordpress_initialized');
});

add_action('wordpress_initialized', function(){
    register_activation_hook('hello.php', function(){
        echo 'Hello is not allowed.';
        die;
    });
});

What i'm doing is:

  1. listening to the init hook
  2. Inside it, listening to the hello.php plugin activation to say it was activated.
  3. Inside the init hook, triggering an custom hook to say wordpress is loaded
  4. Outside, i wait for this another custom hook and then listen to the hello activation
  5. When activated, i block the activation saying it's not allowed.

This is an example, and it works, when the plugin i'm listening is another plugin.

The problem: If i apply this code to listen MY plugin, it will not work. (content below is inside myplugin.php)

add_action('init', function(){
    register_activation_hook('myplugin.php', function(){
        echo 'myplugin was activated.';
    });
    do_action('wordpress_initialized');
});

add_action('wordpress_initialized', function(){
    register_activation_hook('myplugin.php', function(){
        echo 'myplugin is not allowed.';
        die;
    });
});

And this happens because, i wait until the init hook to register the activation hook... If i register the activation hook outside any hook, it will work.

But there is more.

If i trigger an custom action inside the activation hook, this action can be listen.

register_activation_hook('myplugin.php', function(){
    echo 'myplugin was activated.';
    do_action('myplugin_activate');
});

add_action('myplugin_activate', function(){
    echo 'myplugin is not allowed.';
    die;
});

So there is the problem and there is the solution, but what i don't know is WHY, why i can listen to other plugins activation, but can't listen to my own this way? It's because the activation process? I read about this in the docs and an redirection happens, how this affect the working flow?

---EDIT---

The last example isn't exactly an solution. What i need is add an listener to my own plugin inside the init hook like this:

add_action('init', function (){
    add_action('myplugin_activate', function(){
        echo 'myplugin is not allowed.';
        die;
    });
});
1

The init and plugins_loaded hooks are already run before a plugin is activated. That's why your first code doesn't work but the second does.

Regarding your third code: there's no need to run add_action('myplugin_activate' … inside init. Not everything needs to be hooked to init. Just use

add_action('myplugin_activate', function(){
    echo 'myplugin is not allowed.';
    die;
});

without any init stuff. Although I probably wouldn't exactly use die :-)

Check out the Codex article on register_activation_hook() which has many valuable examples.

1
  • I used this as an example. the problem is in my project, the modules are called on the init hook. Basically the init was already triggered but my plugin wasn't loaded right? so if i test with did_action the init and if true, just call the function instead of add the action listener, it should work? – Maxwell s.c Jan 26 '18 at 14:04

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.