Whether you are building a theme for a client, commercial or open source project you may run into a situation where your theme’s functionality depends on a plugin. A good example would be Advanced Custom Fields. This plugin pack a full array of customization options for WordPress that many themes lack. Why not build a theme and require it activated to use? That’s exactly what I’m doing, and here’s how!

Enter TGM Plugin Activation

TGM Plugin Activation is the best way to require plugins for WordPress themes.

TGM Plugin Activation

How to Add it to Your Theme

1. First, head over to the TGM Plugin Activation GitHub repo and download a zip or fork the code.

TGM Plugin Activation is a PHP library that allows you to easily require or recommend plugins for your WordPress themes (and plugins).

2. Include the class in your theme’s function.php file (or wherever you keep that magic) like so:

3. Check out the example.php file over at the GitHub repo. It’s very well commented and within a few minutes I had a requirement for Advanced Custom Fields and it’s add-on ACF Repeater:

Now when I activate my theme without these plugins activated I see:

This theme requires the following plugins

Begin Installing Plugins

Imagine you are the end user. The next thing they are going to do is most likely install the plugins. Let’s see how TGM handles this:

Install Required Plugins

I love the native WordPress UI look of this screen. It’s very professionally done and is familiar to end user and easy to navigate. Now, the user should download the plugins.

Download Required Plugins

Bonus: If the plugin is required, it is automatically activated. As well, the user won’t be able to delete the plugins without first switching themes or removing them via FTP. Pretty slick.

Also, if the plugin is required and the user tries to deactivate it – it won’t work. This is neat, although I would like to see the required plugins appear under a “Must-Use” tab. Apparently, this has already been discussed and isn’t in there and I wouldn’t expect it anytime soon.

Wrapping It Up

The TGM Class is very handy and I’m grateful for the author providing this to the community.

The TGM Plugin Activation library was created and is maintained by Thomas Griffin and Gary Jones.

Devin Walker is a San Diego-based WordPress Developer and enthusiast. He is the author of several popular and highly-rated WordPress themes and plugins. In his free time he enjoys playing Golf and traveling.

Follow Devin:

3 Responses to “How to Easily Require Plugins for Your WordPress Themes”

  1. krunallets

    i have add this code in function.php

    and add two file in my theme but its not working

    Ex. class-tgm-plugin-activationphp

    And

    Ex.example.php

    Download from here
    http://tgmpluginactivation.com/#installation

    please help me

    Reply
  2. Archy

    Here’s a more elegant solution (no update required on long term and no possible vulnerability) https://github.com/ThemeFuse/Unyson/issues/517

    Reply
  3. mfissehaye

    Thank you. That was very helpful.

    Reply

Leave a Reply