Adding Plugins to Themes or Other Plugins
If you are a WordPress theme or plugin developer, you may eventually like to include a specific plugin in your project. For example, I have been working on a child theme for the Genesis Framework and I wanted to include a plugin from the WordPress plugin repository in that child theme. I wanted the user to be able to easily install the plugin right from the WordPress dashboard without having to search the repo. If you’ve used feature-rich themes, you may have seen a message prompting you to install one or more plugins that the theme either requires or suggests (see the image above). I wanted to do this with my child theme to make it easy on the user. After a bit of searching, I found the TGM Plugin Activation PHP library from Thomas Griffin, a well-known expert WordPress developer. His script allows you to display a message when a user installs your theme (or plugin) to inform them that it requires or recommends specific plugins (see image above).
TGM Plugin Activation is a PHP library that allows you to easily require or recommend plugins for your WordPress themes (and plugins). It allows your users to install and even automatically activate plugins in singular or bulk fashion using native WordPress classes, functions and interfaces. You can reference pre-packaged plugins, plugins from the WordPress Plugin Repository or even plugins hosted elsewhere on the internet.
How to Use TGM Plugin Activation
To begin, download the TGM Plugin Activation zip file. This contains the files you need to get started. Once you’ve done that, you need to include the class-tgm-plugin-activation.php file somewhere in your theme or plugin. For my child theme, I just created a “lib” folder in the root and copied the file into that.
Next, you need to add and configure the file that will be including the plugin(s) you want to add. Along with the class file that was included in the zip file you downloaded is a file called example.php. I moved and renamed this file to the lib directory with the class file I moved previously. In the example.php file, Thomas gives you three different examples of adding a plugin: a pre-packaged plugin, a plugin from a private repo such as Amazon S3, and a plugin from the WordPress plugin repo. For a WordPress repo plugin, you only need to pass in the plugin name, slug, and whether it is required (default is set to false, which makes it “Suggested”). For my scenario, the plugin name is “Genesis Connect for WooCommerce” and the slug is “genesis-connect-woocommerce”. I also wanted to make the plugin “suggested” rather than required so I left the required argument set to false. There are also a bunch of other you can configure so take a look at what you can do. For me, I just wanted the plugin to activate automatically after it was installed so I set the
is_automatic argument to true. Once I was finished configuring this file, I renamed it engagewp_plugins.php but you can name it whatever you want (I suggest giving it a unique name).
Finally, I included the newly-renamed engagewp_plugins.php file inside my child theme’s functions.php by using
include_once dirname( __FILE__ ) . '/lib/plugins.php';. Including this file in your functions.php is a requirement. In his documentation, Thomas says to use a
require_once call to include the file but I went with the include call to avoid throwing any fatal errors if the file gets deleted for any reason. Once you’ve included this file in your functions.php, you are all set.
- Downloaded the TGM Plugin Activation zip file
- Moved the class file to a “lib” directory inside the root of my theme
- Moved the example.php file to my includes folder/li>
- Configured, saved, and renamed the example.php file to engagewp_plugins.php
- Included the file in my functions.php using include_once dirname( __FILE__ )
There you have it! If you have any questions or comments, feel free to leave them below.