Creating Dependent WordPress Plugins

By Glenn Ansley  |  June 11th, 2009  |  Published in Projects  |  8 Comments

This script was generated in response to a thread on the wp-hackers mailing list.

The question at hand is how to make one plugin dependant on another plugin’s installation and activation while maintaining the use of the register_activation_hook() function.

The following code is a proof of concept. It can be improved and will be improved after I get through with WordCampRDU this weekend. I slapped it out while on a 2 1/2 hour phone call with a client and only had time to confirm it works and post it here.

How to use this proof of concept:

  1. Copy the code below and paste it into the top of “Hello Dolly”
  2. Try to activate “Hello Dolly” without “Akismet” activated.
  3. Activate “Akismet” and try to activate “Hello Dolly”
  4. Deactivate “Akismet” and confirm that “Hello Dolly” has been deactivated

Still To Do:

  1. Investigate the active plugins option to see if I can prevent hardcoding the plugin’s file location
  2. Enable a way to notify the admin that a dependent plugin has been deactivated at the same time the required plugin was deactivated.
  3. Copy the WordPress Core’s implementation of deactivating a plugin to allow for multiple dependencies and to take advantage of existing action hooks.

The Code: ( a modified hello.php )

Responses

  1. scribu says:

    June 11th, 2009 at 4:40 pm (#)

    Yes, it works because the plugins aren't actually using any functions defined by the other. Try to add a new function in Hello Dolly and then call it from akismet, and then you'll see what I mean. (Akismet being the child plugin).

  2. Glenn says:

    June 11th, 2009 at 5:03 pm (#)

    Okay, so my script as it stands gets rid of the need for step 1 in your original email (here for others reading: http://groups.google.com/group/wp-hackers/browse_... and it answers the problem you have in step 2.

    So, about the other problem:

    I added a function to Hello Dolly and then I went over to Akismet and tried to call it.

    I understand that its going to die if its referenced before its defined. My question is… in what scenario would that need to happen? Meaning, whatever you're trying to do when Akismet loads, why can't place it in an action and call it just a little bit later?

  3. scribu says:

    June 11th, 2009 at 7:58 pm (#)

    That’s also an interesting solution, but it doesn’t solve the loading problem:

    If “Hello Dolly” was instead named “AA Hello Dolly”, it would be loaded before Akismet and not work.

  4. Glenn says:

    June 11th, 2009 at 8:33 pm (#)

    Did you try it? It works for me. The script must go into the plugin that is dependent.

    First, switch the “dependent” and “required” plugin variables. Second, put the script in Akismet instead of Hello Dolly and you can’t activate Akismet unless Hello Dolly is activated.

  5. scribu says:

    June 11th, 2009 at 9:10 pm (#)

    Because of register_activation_hook() and the like.

    If you know of a hook that fires before register_activation_hook(), but after all plugins are loaded, please let me know.

  6. Glenn says:

    June 11th, 2009 at 9:23 pm (#)

    Well, we both know that doesn’t exist. I’m not trying to say a patch isn’t necessary here… just looking for solutions until that point (I need them too).

    If I’m hearing you correctly to this point – and I think I am – the only place this wouldn’t work for you with WP 2.8 is on the register_activation_hook(). Why not take whatever the guts of your activation hook are and postpone them a couple of seconds.

    ie:
    register_activation_hook( __FILE__ , ‘old_function’);
    function old_function(){
    add_action(‘plugins_loaded’ , ‘new_activation_function’);
    }

    In that scenario, new_activation_function() would only be called if the plugin was being activated (that’s the point, right) and, it would be called, after all the other plugins’ functions are available.

    I understand the argument “We need a patch” and agree, but I believe this would work in the mean time.

  7. scribu says:

    June 11th, 2009 at 10:00 pm (#)

    Sadly, that doesn’t seem to work:

    register_activation_hook(__FILE__ , ‘old_function’);
    function old_function() {
    add_action(‘plugins_loaded’ , ‘new_activation_function’);
    }

    function new_activation_function() {
    echo “etcetera”;
    add_option(‘__new_activation_function’, ‘bla’);
    die;
    }

  8. kyb3lion says:

    January 5th, 2010 at 3:16 pm (#)

    Can someone tell me how to deactivate Akismet? I've been looking all over the internet, and the official sites (WordPress and Akismet developers) just stone-wall the blog owners for years.

Leave a Response