4

I'm looking into using http://tgmpluginactivation.com/ to activate required plugins for my custom theme during the theme activation step.

From what I can tell, this class only Activates plugins automatically, but it doesn't automatically Install plugins I have bundled with my theme.

Does anyone know if it's possible to automatically Install plugins upon theme activation? Preferably, in a way that can still make use of TGM?

3
  • How are you bundling these plugins? Are they included in the download with instructions to add theme to wp-content/themes or are you including them in the theme's directory?
    – JPollock
    Jul 19, 2013 at 2:12
  • @JPollock they are packaged with the theme as individual zip files. TGM is configured to look at that directory when installing. Everything works fine when the user manually clicks the Install plugin link after theme activation, but I was looking for an automated way to install the plugins without any user action required.
    – teamcrisis
    Jul 19, 2013 at 10:06

2 Answers 2

3

Edited Answer:

TMG is a very popular auto plugin installer for WordPress and a lot of Premium theme author use it. You can get the php class here https://github.com/thomasgriffin/TGM-Plugin-Activation. Also when you will download it you will get a php file named example.php. You just have to include that example.php file in your function.php file and you just can edit that file to have auto installation for the required plugins for your theme.

You have to do something like this in the example.php file

// This is an example of how to include a plugin pre-packaged with a theme.
        array(
            'name'               => 'WpMania Slider', // The plugin name.
            'slug'               => 'WpmSlider', // The plugin slug (typically the folder name).
            'source'             => get_template_directory_uri() . '/assets/plugins/plugins/WpmSlider.zip', // The plugin source.
            'required'           => true, // If false, the plugin is only 'recommended' instead of required.
            'version'            => '', // E.g. 1.0.0. If set, the active plugin must be this version or higher.
            'force_activation'   => true, // If true, plugin is activated upon theme activation and cannot be deactivated until theme switch.
            'force_deactivation' => true, // If true, plugin is deactivated upon theme switch, useful for theme-specific plugins.
            'external_url'       => '', // If set, overrides default API URL and points to an external URL.
        ),

Thanks Sabbir

4
  • 3
    Where would that array go? Not everyone is familiar with TMG, and what does it stand for?
    – Tom J Nowell
    Apr 11, 2014 at 11:07
  • TMG is a very popular auto plugin installer for WordPress and a lot of Premium theme author use it. You can get the php class here github.com/thomasgriffin/TGM-Plugin-Activation. Also when you will download it you will get a php file named example.php. You just have to include that example.php file in your function.php file and you just can edit that file to have auto installation for the required plugins for your theme.
    – Sabbir H
    Apr 11, 2014 at 15:04
  • Nice, you should edit your answer to include that
    – Tom J Nowell
    Apr 11, 2014 at 15:12
  • 1
    I have updated the answer upon request
    – Sabbir H
    Apr 12, 2014 at 9:38
2

The activate_plugin function should be what you're looking for. Place it in a after_setup_theme action.

Usage

activate_plugin ( $plugin, $redirect = '', $network_wide = false, $silent = false )

The doc. page for the activate_plugin function doesn't look too promising, and I have no idea if it will actually work, since I've never used it. I guess it's worth a shot?

I imagine activating plugins when a theme is activated could be potentially wreak havoc and break websites.

References

1
  • Surely making a plugin mandatory can make your theme weak, that is why there should be a fallback. In my previous answer I recommend you to use TMG plugin. So there is a option so that you can make a plugin required ( please check my Answer above as example ). If you just make the "required" to "false" you can overcome that situation.
    – Sabbir H
    Apr 13, 2014 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.