getButterfly
Resume

NAVIGATION

ATTRIBUTION

getButterfly
Dublin, Ireland

All Content Copyright ©2019
getButterfly

How to Install WordPress Plugin Dependencies

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 customisable from a design point of view.

Recommended WordPress plugin dependencies.
Recommended plugin dependencies.

Here’s the main function:

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

    foreach ($pluginArray as $recommendedPlugin) {
        $name = $recommendedPlugin['name'];
        $slug = $recommendedPlugin['slug'];
        $file = $recommendedPlugin['file'];

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

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

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

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

<?php
$recommendedPlugins = [
    [
        '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($recommendedPlugins);
?>

I might revisit the code later to make it more modular and more functional (as in Functional Programming) codewise.

Added by Ciprian on Friday, July 5, 2019 in Blog

Do you want better SEO? More traffic? More conversions? More growth? We help companies exponentially grow their traffic and conversions, while outranking their competitors. With more than 10 years of experience, we’ve learned what is valuable to our clients.
SEO Dublin | SEO Malta

My Battle Tested Recommendations

Jetpack
Dreamhost
WordPress.com
CodeCanyon
SEMrush

Disclaimer: These recommendations contain affiliate links.


Privacy Policy