How would you require and automatically download dependent plugins?

Was wondering if there was a class or programming technique that would allow me to check for the installation and activation of a particular plugin and if said plugin were not installed, to have it downloaded from WordPress’s plugin repository?

I have a plugin I would like to require scribu’s Post 2 Post plugin and I was wondering how it could be done?

Related posts

Leave a Reply

3 comments

  1. I would encourage against this, but I understand what you’re trying to do and do something similar myself.

    How I do it

    I build themes that depend on plugins, plugins that depend on plugins, and plugins that depend on plugins that depend on other plugins. If I’m controlling both sides of the development, I do things in two pieces …

    In the plugin that will be required by something else:

    add_filter( 'my-cool-plugin-name-installed', '__return_true' );
    

    In the plugin/theme that will require the other plugin:

    if ( ! apply_filters( 'my-cool-plugin-name-installed', false ) )
        add_action( 'admin_notices', 'my-cool-plugin-name_not_installed' );
    

    Then I add a bright “Please install my super-cool plugin” notice to the top of the admin screen with a link to the download page.

    This gives me a surefire way to check that my dependencies exist and are installed. If the plugin is installed but not activated, the warning still shows up.

    Another way

    Another option has already been recommended by @tollmanz. I won’t copy-paste his solution, but checking for the existence of a core function of your dependent plugin is a great way to make sure it’s there.

    Once again, if the plugin is installed but inactive, this route will only detect if it’s active.

    Why I do things this way

    First of all, I don’t like other people’s tools downloading extra stuff to my site. So I don’t force that paradigm on other users. Instead, I prompt them to download the extra code and point them in the right direction. A cleaner way would be to tie in with the automated installer so they could pull down the plugin with a single click.

    Also, several users of my code are on servers where they can’t use the one-click installer (or automated downloads at all). They have to FTP plugins to install them, so a silent download-and-activate-a-dependency system wouldn’t work at all.

    Finally, if you’re working with someone else’s code, you have 0 control over when they ship new releases, if they introduce bugs, or if they’re hacked. So don’t automatically install code for which you can’t claim responsibility.

  2. My method to handle a similar situation was to look for a core class or function in the plugin dependency and check if it exists. For instance, I built a plugin that depended upon the Simple Term Meta plugin. I checked for it using:

    if(!function_exists('simple_term_meta_install'))
    {
        // Do stuff to download plugin
    }
    

    In my plugin, I simply packaged the plugin with my plugin. I would only run it if it wasn’t already installed.

    Your other option would be to prompt the user to download the plugin and install.

    In terms of how you would download it automatically…I do not know. I would look to see how the WP core manages this and try to glean some knowledge from the built in system.

  3. Following up on EAMann’s notification idea, you can add a link in an admin notice that will take the admin to a search result page in Add Plugin. Since the name specified is the exact name it will be listed first in the results. All that is then needed is to click the Install Now link.

    function posts_2_posts_required() {
        $url = network_admin_url( 'plugin-install.php?tab=search&type=term&s=Posts+2+Posts&plugin-search-input=Search+Plugins' );
        echo '
        <div class="error">
            <p>The <a href="' . $url . '">Posts 2 Posts Plugin</a> is required.</p>
        </div>
        ';
    }
    
    function check_required_plugins() {
        if ( current_user_can( 'activate_plugins' ) ) {
            include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
            if ( ! is_plugin_active( 'posts-2-posts/posts-2-posts.php' ) )
                add_action( 'admin_notices', 'posts_2_posts_required' );
        }
    }
    add_action( 'plugins_loaded', 'check_required_plugins' );
    

    To have access to current_user_can() you need to wait until the plugins are loaded, hence the plugins_loaded action. The network_admin_url() will return the correct URL regardless if your site is setup as multisite or not.