WordPress.org

Ready to get started?Download WordPress

Forums

(De-)Activate some plugin when (de-)activating another one (5 posts)

  1. Ahaenor
    Member
    Posted 1 year ago #

    Greetings,

    i've encountered some difficult situation in my plugin development.

    My plugin is an add-on on the other one, that completely changes it's functionality.

    I want to activate this another plugin (I will call it "base") when activate my plugin (call it "add-on"). Likewise, I want to deactivate "base" when "add-on" deactivates.

    I've tried to do this:

    // Add-on activation routine:
    register_activation_hook('add-on.php', function(){
        if( !is_plugin_active('base-plugin/base-plugin.php') ) {
            activate_plugin(WP_PLUGIN_DIR.'/base-plugin/base-plugin.php');
    });

    and, likewise, to use
    deactivate_plugins(WP_PLUGIN_DIR.'/base-plugin/base-plugin.php');

    in register_deactivation_hook('add-on.php');

    But both cases don't work, or work in some strange ways. I can describe it in more details, if needed. But, maybe, there's some simple way to make it possible.

    Thnx in advance.

  2. bcworkz
    Member
    Posted 1 year ago #

    That should be about all it takes, not sure what the problem is. The plugins.php page does just that, then unsets the plugin array key/value stored in the option 'recently_activated'. I can't imagine that option would make or break activation though.

    You may want to avoid anonymous functions for now though, they only work with PHP versions since 5.3, and WordPress still allows slightly earlier versions.

  3. Ahaenor
    Member
    Posted 1 year ago #

    Hello, and thnx for your quick answer.

    No, anonymous functions are definitely not the case: my server has php 5.3+, and I'm using them all around without prob.

    The behavior while this operations is quite strange. I think the problem is that "init" or "admin_init" hook in add-on plugin is triggered earlier than "register_activation_hook". It causes the base plugin and add-on conflict.

    Is there a way to trigger activation/deactivation hook earlier than init? I've searched the codex, but hadn't find a clue.

  4. bcworkz
    Member
    Posted 1 year ago #

    I suppose you could find an action to hook before init, but WP would be somewhat unstable so the effort may be a failure. I don't think the earliness is the issue, but I do think you're on the right path. Take a look at this bit about activation hooks and filters. Perhaps the 'recently_activated' option is critical to the process after all, though not necessarily that particular option, rather more the concept.

    In other words, you should try devising a way to cause the activate_plugin() call to occur during the next page load instead of inside your activation hook. Seems worth a try at least.

  5. Ahaenor
    Member
    Posted 1 year ago #

    Ok, thanks again for idea and reference. I've come to a similar thoughts, will try something like that.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.