How to Install WordPress Plugin Dependencies

Ciprian on Friday, July 5, 2019 in Blog

NEW! Learn JavaScript by example. Code snippets, how-to's and tutorials. Try now!

This is a lightweight alternative to TGM Plugin Activation class, which is currently the best way to require and recommend plugins for WordPress themes (and other plugins), according to the TGMPA homepage.

My code is smaller and more concise, while being extendable and customizable from a design point of view.

Recommended WordPress plugin dependencies.
Recommended plugin dependencies.

Here’s the main function:

<?php
function supernova_register_required_plugins( $plugin_array ) {
    $action = 'install-plugin';

    foreach ( $plugin_array as $recommended_plugin ) {
        $name = $recommended_plugin['name'];
        $slug = $recommended_plugin['slug'];
        $file = $recommended_plugin['file'];

        $link = wp_nonce_url(
            add_query_arg(
                [
                    'action' => $action,
                    'plugin' => $slug,
                ],
                admin_url( 'update.php' )
            ),
            $action . '_' . $slug
        );

        $status        = ' (not installed)';
        $button_status = '';

        if ( file_exists( WP_PLUGIN_DIR . '/' . $file ) ) {
            $status        = ( is_plugin_active( $file ) ) ? ' (active)' : ' (inactive)';
            $button_status = ( is_plugin_active( $file ) ) ? 'button-active' : 'button-inactive';
        }

        echo '<a href="' . $link . '" class="button button-secondary ' . $button_status . '">' . $name . $status . '</a> ';
    }
}
?>

And here’s the function call inside your plugin or theme settings panel:

<?php
$recommended_plugins = [
    [
        'name' => 'Yoast SEO',
        'slug' => 'wordpress-seo',
        'file' => 'wordpress-seo/wp-seo.php',
    ],
    [
        'name' => 'CMS Tree Page View',
        'slug' => 'cms-tree-page-view',
        'file' => 'cms-tree-page-view/index.php',
    ],
    [
        'name' => 'Intuitive Custom Post Order',
        'slug' => 'intuitive-custom-post-order',
        'file' => 'intuitive-custom-post-order/intuitive-custom-post-order.php',
    ]
];

supernova_register_required_plugins( $recommended_plugins );
?>

Related posts

2 comments on “How to Install WordPress Plugin Dependencies

  1. Hey,

    Thanks for a great alternative to the TGM Plugin.

    Is there a way to include external plugins too?

    Many thanks,
    Paul

    1. You would probably need to fetch and unzip them from any external source, such as a URL, a bundled plugin or a public GitHub repository.

      Maybe with the upcoming changes to WordPress bening able to update plugins from non-WordPress.org locations, it might work out of the box with minimal code changes.

Leave a Reply

Your email address will not be published. Required fields are marked *