Installing Plugins Automatically in a Premium WordPress Theme
I think those who have ever downloaded WordPress themes or bought them on exchanges such as themeforest, noticed that after installation and activation, many themes ask you to install the necessary plugins for them. Today we will talk about the library TGM Plugin Activation which allows you to install plugins after activating the theme, learn how in premium themes all plugins are set automatically. Plugins can be installed both from the WordPress repository, and other external sources, it will also be possible to install plugins that you developed and put in your theme. This library class installs and activates plugins based on the WordPress API., which means that there should be no problems with the update.
How to set up TGM Plugin Activation
We will carry out the settings on the standard TwentyEleven theme, but you can use another theme, check only that in your theme in style.css file, comment has been added: Text Domain: twentyeleven. Below in the skin we will use it in a variable $theme_text_domain = ‘twentyeleven’;.
First we need to download the library from developer site or from GitHub and put the file in the root of our theme class-tgm-plugin-activation.php and folder plugins with all its contents.
After the necessary files are copied, we need to connect our class to the template. We connect the class through a file functions.php.
To the functions.php file, at the top of it, add the code for calling the function to install required plugins:
1 2 |
require_once dirname(__FILE__).'/class-tgm-plugin-activation.php'; add_action('tgmpa_register','my_theme_register_required_plugins'); |
Below we add the code of the function itself:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
function my_theme_register_required_plugins(){ $plugins = array( /* установка плагинов из папки темы, которые вы поставляете в комплекте */ array( 'name' => 'TGM Example Plugin', // имя плагина 'slug' => 'tgm-example-plugin', // Имя плагина (совпадает с именем папки плагина) 'source' => get_stylesheet_directory().'/plugins/tgm-example-plugin.zip', // источник плагина 'required' => false, // 'required' - плагин обязательый? нужно ли го активировать поле установки? ), /* установка плагинов на автомате из репозитория wordpress */ array( 'name' => 'Edit Howdy', 'slug' => 'edit-howdy', // слуг можно посмотреть в ссылке к плагину // https://wordpress.org/plugins/edit-howdy/ - edit-howdy ), array( 'name' => 'Evanto dashboard', 'slug' => 'evantodashboard', 'source' => get_stylesheet_directory().'/plugins/evantodashboard.zip', // источник плагина 'required' => false, ), ); $theme_text_domain = 'twentyeleven'; // текстовый домен темы $config = array( /*domain => $theme_text_domain, // текстовый домен, точно такой как указан в вашей теме*/ /*dafault_path => '', // Абсолютный путь по умолчанию к папке плагинов*/ /*menu => 'install-my-theme-plugin', // Menu slug*/ 'settings' => array( /*'page_title' => __('Install Required Plugins', $theme_text_domain)*/ /*'menu_title' => __('Install Plugins', $theme_text_domain)*/ /*'instructions_install' => __( 'The %1$s plugin is required for this theme. Click on the big blue button below to install and activate %1$s.', $theme_text_domain ), // %1$s = plugin name */ /*'instructions_activate' => __( 'The %1$s is installed but currently inactive. Please go to the <a href="%2$s">plugin administration page</a> page to activate it.', $theme_text_domain ), // %1$s = plugin name, %2$s = plugins page URL */ /*'button' => __( 'Install %s Now', $theme_text_domain ), // %1$s = plugin name */ /*'installing' => __( 'Installing Plugin: %s', $theme_text_domain ), // %1$s = plugin name */ /*'oops' => __( 'Something went wrong with the plugin API.', $theme_text_domain ), // */ /*'notice_can_install' => __( 'This theme requires the %1$s plugin. <a href="%2$s"><strong>Click here to begin the installation process</strong></a>. You may be asked for FTP credentials based on your server setup.', $theme_text_domain ), // %1$s = plugin name, %2$s = TGMPA page URL */ /*'notice_cannot_install' => __( 'Sorry, but you do not have the correct permissions to install the %s plugin. Contact the administrator of this site for help on getting the plugin installed.', $theme_text_domain ), // %1$s = plugin name */ /*'notice_can_activate' => __( 'This theme requires the %1$s plugin. That plugin is currently inactive, so please go to the <a href="%2$s">plugin administration page</a> to activate it.', $theme_text_domain ), // %1$s = plugin name, %2$s = plugins page URL */ /*'notice_cannot_activate' => __( 'Sorry, but you do not have the correct permissions to activate the %s plugin. Contact the administrator of this site for help on getting the plugin activated.', $theme_text_domain ), // %1$s = plugin name */ /*'return' => __( 'Return to Required Plugins Installer', $theme_text_domain ), // */ ), ); tgmpa( $plugins, $config ); } |
If you did everything right, you will see a message saying, what plugins need to be installed, required for this topic., message will look like this.
Click on the button Begin installing plugins and you will see a window with a list of your plugins.
In the window we can see, that the Edit Howdy item appears as a link, by clicking you will be able to read about this plugin, since we install it from the repository. Other plugins will be inactive, since they will be installed from the theme folder.
Let's start the automatic installation of plugins
- Select plugins to install,
- Select the action "install",
- Click Apply.
If everything was done right, then you have 2 plugins will be installed and one will display an error (An error occurred while installing Evanto dashboard: Download failed. Invalid URL provided..). The error appeared because we did not add the Evanto dashboard plugin file to our theme.
It remains only to mark all the necessary plugins and activate them.
If everything was done right, then you will get a message (The following plugins were activated successfully: Edit Howdy and TGM Example Plugin).
If you have not installed all plugins and decide to install one by one, while doing this you lost the installation window, then you can always return to the installation page through the admin menu in the section Appearance -> Install Plusins.
That's all, trying to install our plugins! Good luck in developing user-friendly and high-quality themes for WordPress.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.