TGM Plugin Activation: automate the installation of plugins WordPress [Quick Guide]

7:36 pm February 20, 201714875

If you are freelance WordPress and you regularly make sites for your clients, you certainly have a list of plugins that pamper you and that you are installing to each new creation.

However, this redundancy can sometimes become boring and you waste time unnecessarily… If this is your case, then you have come to the right place as we will see together how to automate the installation of your plugins favorite thanks to the TGM Plugin Activation .

intro tgmpa

What is that the TGM Plugin Activation?

The TGM Plugin Activation was created and developed by Thomas Griffin , a WordPress expert, to whom we owe the creation of OptinMonster and EnviraGallery among other… Thanks to this plugin and a few lines of code you can start the installation and activation of your plugins favorite from activation of your Theme / Theme Premium or Child Theme : it is you who decide where to locate it. The purpose of this feature is to save valuable time when creating a new WordPress site.

How does it work?

The TGM Plugin Activation is not a plugin available on WordPress plugins directory. We have to go through a few steps and put my hands in the code to make it work but the result is so good that it’s worth! For this tutorial, I established the TGM Plugin Activation in a Child Theme Let’s see together how do…

Step 1: create a child theme

this plugin will require writing PHP code in the file functions.php . If it is not you who create your WordPress theme and that you use an existing theme, it is highly recommended to go through the creation of a child theme which will protect so your changes in the updates of the topic parent. If you have never created a child theme, you can find procedures in this article .

Step 2: download TGM Plugin Activation

after you have created your child theme, it will need to download the TGM Plugin Activation on the page .

Télécharger le TGM Plugin Activation

To do this, it will choose an option to use (Theme / Child Theme / Plugin) then fill in the three fields (Text Domain / Function Prefix / Name) before clicking on Generate . Will then begin to download the TGM Plugin Activation files and you can get back them in your computer’s download folder.

Step 3: locate the TGM Plugin Activation in the child theme

once downloaded, you must decompress the zipped folder to extract the file named class-tgm-plugin – activation.php

Extraire le fichier class-tgmpa

then you need to copy this file in the folder of your child theme directly to the root or in a sub folder . So far, not rocket science!

Step 4: Edit the file functions.php of the child theme

in the file functions.php you can save all the necessary plugins to your Child Theme using a text editor such as SublimText for example. And it is also here that it will take to handle the code… Before you start the explanations, here is what your file will look like:

Code PHP pour le TGM Plugin Activation

  1. as a first step, we need to appeal to the file class-tgm-plugin – activation.php recently added in your child theme (step 3). either the lines 10 to 14 of the screenshot above. As a reminder, all these lines will be to add following code already present in your file functions.php . Here is the raw code:
     require_once dirname (__FILE__). ' / class-tgm-plugin - activation.php';
    add_action ('tgmpa_register', 'enregistrer_mes_plugins'); 
  2. Secondly, we need to declare a new function. In my example, this is the function enregistrer_mes_plugins that starts at line 18 and ends on line 68 of my screenshot. Below is the raw code:
     function enregistrer_mes_plugins() {$plugins = array (/ / example: to include a pre-package plugin in a theme / / example B: to include a plugin from an external directory like GITHUB / / example C: to include a plugin available in the directory of WP plugins);}
    
    $theme_text_domain = 'twentyseventeen'; Change to the text-domain of the theme $config = array ('domain' => $theme_text_domain, / / Text domain - the same as your theme 'default_path' => ", / / absolute path by default for plugins pre-packaged 'menu'-online 'install-my-theme-plugins', / / Menu slug 'strings'-online array ('page_title'-online _ ('Install the recommended plugins', $theme_text_domain), / / 'menu_title'-online _ ('Installing Plugins', $theme_text_domain), / / 'instructions_install'-online _ (' the plugin %1$ s is recommended for this theme.))) Click the button to install and activate %1$ s.', $theme_text_domain), / / %1$ s = name of the plugin 'instructions_activate'-online _ (' the plugin %1$ s is installed but inactive.) Go to  d  administration page to its activation.', $theme_text_domain), / / %1$ s = name of the plugin, %2$ s = plugins page URL 'button'-online _ ('Install %s now', $theme_text_domain), / / %1$ s = name of the plugin 'installing'-online _ (' Installation of the Plugin: %, $theme_text_domain), / / %1$ s = plugin name 'oops'-online _ ('a s error occurred.', $theme_text_domain), / / 'notice_can_install'-online _ (' this topic recommend the plugin %1$ s.   Click here to start installation  .') (, $theme_text_domain), / / %1$ s = name of the plugin, %2$ s = TGMPA page URL 'notice_cannot_install'-online _ (' sorry, you do not have the necessary permissions to install the %1 plugin $s.', $theme_text_domain), / / %1$ s = name of the plugin 'notice_can_activate'-online _ (' this theme need the plugin %1$ s.) Currently inactive, you must go on  d administration of the plugin page  for l. Activate ', $theme_text_domain), / / %1$ s = plugin name, %2$ s = plugins page URL 'notice_cannot_activate'-online _ (' sorry, you do not have the permissions necessary to activate the plugin %1$ s.', $theme_text_domain), / / %1$ s = plugin name 'return'-online _ ('return to the installer of plugins' $theme_text_domain),),);
     tgmpa ($plugins, $config);
    } 

    so far here, you have to just copy and paste the above code without forgetting to change the theme_text_domain on line 46 of my screenshot. For my example, I informed ‘twentyseventeen’ my child theme is linked to the theme Twenty Seventeen.

  3. Finally, you will register your various plugins . In my example, the check of my plugins starts at line 21 of my screenshot ends at line 43.  Note that each plugin to build an ARRAY (table), which is itself included in an ARRAY as you can see start line 20 to end at line 44 of my screenshot. The call of your plugins will depend on and will vary slightly of the original source of these, three cases are then available to you:
  • example A: include a “pre-package” plugin
    it could be premium plugins that you have the proper license to be used on multiple sites. In my example, this is the plugin WP Rocket . To do this, it will include the dossier.zip this plugin in your child theme folder: either in the root or in a subfolder. In the line ‘source’ , it will then indicate the relative path (as in line 25 of the screenshot). Here is the code to add:
     array ('name'-online 'WP Rocket', / / the name of the plugin 'slug' => ' wp - rocket_2.8.23', / / the slug of the plugin (usually the name of the folder) 'source'-online get_stylesheet_directory().) ('/wp-rocket_2.8.23.zip', / / relative path of the plugin to format .zip 'required'-online false, / / FALSE means that the plugin is only recommended), 
  • example B: appeal to a plugin in an external directory
    in this case, it may be plugins contained in a library like GitHub. For my example, I used the example of Thomas Griffin plugin. In the source line, he will have to enter the URL of the plugin (as on line 33 of the screenshot). Here’s how to import it:
     array ('name'-online 'TGM New Media Plugin', / / the) the name of the plugin.
     ('slug'-online 'tgm-new-media-plugin', / / the slug of the plugin (usually the name of the folder) 'source'-online 'https://s3.amazonaws.com/tgm/tgm-new-media-plugin.zip', / / the source of the 'required'-online true plugin, / / TRUE means that the plugin is required 'external_url'-online 'https://github.com/thomasgriffin/New-Media-Image-Uploader', / / if defined, replaces the default API URL, and points to an external URL), 
  • example C: import a plugin available in the official directory of WordPress
    it is here all present plugins in the official directory of WordPress plugins and have you used to install manually via the tab EXTENSIONS > add . For my example, I took the extension MailPoet . Attention, sometimes ‘slug’ is different from the ‘name’ . Here’s how I included it in my Child Theme:
     array ('name'-online 'MailPoet', / / the 'slug'-online 'wysija-newsletters' plugin name, / / the slug of the plugin (usually the name of the folder) 'required'-online false, / / FALSE means that the plugin is only recommended), 

step 5: pass in the administration of your WordPress site

that is, the more “difficult” being made , you can then send via FTP, your full child theme folder wp-content > themes at your shelterer. Therefore, you can go in the administration of your site to activate your child theme via the tab appearance > Themes .

Activation du Child Theme

As soon as the theme, an informative insert will appear at the top of the window that will list the plugins required and recommended depending on what you get parameter in your file functions.php . To begin installation, you won’t have to click Begin Installing Plugins which will lead you to the installation of the plugins page.

Commencer l'installation et l'activation avec TGMPA

With ease, you can then install various plugins by checking and then positioning the drop on Install and apply . Click return to the installer of Plugins to perform the same operation in order to enable all extensions at the same time. Now, if you take a look on the side of the tab Extensions > Extension installed you’ll see all your plugins are present and functional… As if by magic!

in conclusion…

This TGM Plugin Activation is very convenient and valuable for developers of premium themes boarding usually a whole bunch of plugins required and/or recommended with their theme. Can you imagine buying a premium theme that you provide a simple list of plugins to install manually? No! Now that you know this trick, you can concoct your own Child Theme that takes all plugins which you consider useful . You will have to click to install and activate all: less effort to increase productivity… Yes!

For further (still):