Using the TGM Plugin Activation Library in Your Themes

Themes aren't meant to be functional, but as theme developers, we mostly need to include some features to make our theme a bit better and a function, you know, functional. 

In this tutorial, we're going to have a glance at the term "plugin territory" and learn to use a fantastic tool written by Thomas Griffin: the TGM Plugin Activation library.

Theme Functionality: Invading Plugin Territory

Themes are meant to change the design of your WordPress website. Ideally, it should be visual. But in this golden age of WordPress, theme developers often include functional features in their themes in order to stay competitive in the market. This it was supposed to be, but it is.

This is the invasion of the plugin territory. We can define "plugin territory" in simple terms:  Functional parts of code are inside the borders of this territory. Every bit of code that changes the functionality of your website needs to be served as a plugin, if it's not already been served in the WordPress core. 

In one of my previous posts (in the series "Making the Perfect WordPress Theme"), I mentioned the rule of thumb of the "plugin territory":

If the feature is about the visual appearance of the website, it should be in the theme, but if it's about the functionality of a website, it should be included as a separate plugin.

Easy enough, right? 

Although people still tend to hard-code functional bits into their themes, theme directories (like WordPress.org and ThemeForest) are not accepting themes invading the "plugin territory". So, it has become a problem to offer functionality with themes.

Luckily, there is a fairly simple solution and it doesn't go against the "plugin territory" rule.

Introducting the TGM Plugin Activation Library

TGM Plugin Activation is a lightweight library with the purpose of bundling themes with plugins. The idea is simple: When a user installs your theme, it makes the user install plugins from WordPress.org, an external website or the theme folder. Here's what Thomas Griffin, the creator of the library, defines this handy little tool:

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.

This is probably the smartest solution to the "plugin territory invasion" problem. And it's quite easy to apply, too. 

Let's have a look!

Installing TGM Plugin Activation

Installing TGM Plugin Activation is ridiculously easy. Just follow these steps:

  • Download the TGM Plugin Activation library from the "Download" section of the page.
  • Open the zip file and extract class-tgm-plugin-activation.php into your theme folder (anywhere you like).
  • Open your theme's functions.php file and use the require_once() function to, well, require the class file (once) in your theme.
  • Create a function to configure TGM Plugin Activation and hook it to tgmpa_register via the add_action() function.
  • Done!

It's so easy that you don't even need complicated PHP code in order to require or recommend plugins. Take a look at the code below:

From now on, you can make your users install new plugins by setting up the $plugins variable in the function you just created. 

Let's see how it's done.

Installing Plugins with TGM Plugin Activation

As you can see from above, the $plugins variable is an array. And to define plugins to install, you need to create arrays within that array (so you can set their own parameters). Sounds hard, but it's not:

There are a couple of parameters to use:

  • name (string, required) - The name of the plugin.
  • slug (string, required) - The slug of the plugin (usually its folder's name).
  • required (boolean, required) - If it's set to true, your theme will "require" the plugin. If false, the theme will "recommend" it.
  • source (string, sometimes required) - The source of the plugin. If it's a WordPress.org plugin, this parameter shouldn't be used; else, it's required.
  • version (string, optional) - The minimum version required for the plugin. For example; if the theme user has already installed a required plugin but doesn't have the minimum version number you specified, TGM Plugin Activation warns the user to update it.
  • force_activation (boolean, optional) - If set to true, the user will not be able to deactivate the plugin while your theme is active. A bit annoying but it might be necessary in some scenarios.
  • force_deactivation (boolean, optional) - If set to true, the plugin will be deactivated once the user switches themes.
  • external_url (string, optional) - If set, the name of the plugin will be linked to this address in the plugin requirement notice.

You have three options to make your users install plugins with TGM Plugin Activation: You can require a plugin from the WordPress Plugin Directory, from an external source (like your own server or a CDN), or from your theme folder (like /my-theme/plugins/shortcodes.zip).

Requiring a Plugin From WordPress.org

Requiring a Plugin From an External Source

Requiring a Plugin From the Theme Directory

Configuring TGM Plugin Activation

Notice the tgmpa() function with two parameters at the end of our example code? The second parameter is the $config variable which also happens to be an array, just like the $plugins parameter. As the name suggests, you can configure the TGM Plugin Activation library with this array. It also has its own set of options that you need to set:

  • id (string) - A unique ID for the TGM Plugin Activation library that you implemented in your theme. This is actually very important: If another plugin also uses TGM Plugin Activation, the different IDs would prevent conflicts.
  • default_path (string) - The default absolute path for plugins inside your theme. When you set this, you can just use the name of the ZIP file as the source parameter for your plugin.
  • menu (string) - The menu slug for the plugin install page.
  • has_notices (boolean) - If set to true, admin notices are shown for required/recommended plugins.
  • dismissible (boolean) - If set to true, the user can "dismiss" the notices.
  • dismiss_msg (string) - If the dismissable option is set to false, this message will be shown above the admin notice.
  • is_automatic (boolean) - If set to true, plugins will be activated after the user agrees to install them.
  • message (string) - Optional HTML to display before the plugins table.
  • strings (array) - Yet another array that includes the messages to be displayed. You can set them as translatable strings, too. Check out the example.php file to see the full list of message strings.

Wrapping Everything Up

As you can see, it's not at all impossible to offer functionality with WordPress themes – you just have to think of the users when they switch from your theme to another. The TGM Plugin Activation Library offers a really clever way to play it by the book.

What do you think about this tool? Have you ever used it, or are you planning to use it in the future? Tell us what you think by commenting below. And if you liked this article, don't forget to share it with your friends!

Tags:

Comments

Related Articles