Home > Article > CMS Tutorial > Leverage the power of the TGM plugin activation library in your theme
Themes are not meant to be functional, but as theme developers we mainly need to include some features to make our themes a little better and functional.
In this tutorial we will learn about the term "plugin realm" and learn to use an excellent tool written by Thomas Griffin: the TGM plug-in activation library.
Themes are designed to change the design of your WordPress website. Ideally it should be visual. But in the golden age of WordPress, theme developers often included functional features in their themes to stay competitive in the market. It should be so, but it is.
This is an invasion of plugin territory. We can define "plugin domain" in simple terms: the functional part of the code lies within the boundaries of the domain. Every piece of code that changes the functionality of your website needs to be available as a plugin (if it’s not already available in WordPress core).
In one of my previous articles (in the "Making the Perfect WordPress Theme" series), I mentioned a rule of thumb in the "plugin world":
If the feature is related to the visual appearance of the site, it should be in the theme, but if it is related to the functionality of the site, it should be in the theme as a separate Plug-ins are included.
Pretty simple, right?
Although people still tend to hardcode feature bits into their themes, theme directories (such as WordPress.org and ThemeForest) do not accept themes that invade the "plugin realm". Therefore, providing functionality with themes becomes a problem.
Fortunately, there is a fairly simple solution, and it does not violate the "plugin realm" rules.
TGM Plugin Activation is a lightweight library designed to bundle themes with plugins. The idea is simple: when a user installs your theme, it lets the user install plugins from WordPress.org, an external website, or the theme’s folder. Here's how the library's creator, Thomas Griffin, defines this handy little tool:
TGM Plugin Activation is a PHP library that allows you to easily request or recommend plugins for your WordPress themes (and plugins). It allows your users to install and even automatically activate plugins in a single or batch manner using native WordPress classes, functions, and interfaces. You can reference prepackaged 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 easy to apply.
Let’s take a look!
Installing TGM plug-in activation is very simple. Just follow these steps:
class-tgm-plugin-activation.php
to your theme folder (anywhere you like). functions.php
file and use the require_once()
function to request the class file (once) in the theme. tgmpa_register
via the add_action()
function. It's so easy, you don't even need complex PHP code to request or recommend plugins. Take a look at the following code:
<?php /** * Since I'm already doing a tutorial, I'm not going to include comments to * this code, but if you want, you can check out the "example.php" file * inside the ZIP you downloaded - it has a very detailed documentation. */ require_once dirname( __FILE__ ) . '/class-tgm-plugin-activation.php'; add_action( 'tgmpa_register', 'mytheme_require_plugins' ); function mytheme_require_plugins() { $plugins = array( /* The array to install plugins */ ); $config = array( /* The array to configure TGM Plugin Activation */ ); tgmpa( $plugins, $config ); } ?>
From now on, you can let users install new plugins by setting the $plugins
variable in the function you just created.
Let's see how it's done.
As can be seen from the above, the $plugins
variable is an array. To define the plugins to install, you need to create an array within that array (so that you can set your own parameters). It sounds difficult, but it’s not:
<?php $plugins = array( array( /* my first plugin */ ), array( /* my second plugin */ ), array( /* my third plugin */ ), // ... array( /* my nth plugin */ ) ); ?>
There are several parameters available:
name
(字符串,必需)- 插件的名称。slug
(字符串,必需)- 插件的 slug(通常是其文件夹的名称)。required
(布尔值,必需) - 如果设置为 true
,您的主题将“需要”该插件。如果false
,主题将“推荐”它。source
(字符串,有时需要)- 插件的源。如果是 WordPress.org 插件,则不应使用此参数;否则,这是必需的。version
(字符串,可选) - 插件所需的最低版本。例如;如果主题用户已经安装了所需的插件,但没有达到您指定的最低版本号,TGM 插件激活会警告用户进行更新。force_activation
(布尔值,可选) - 如果设置为 true
,当您的主题处于活动状态时,用户将无法停用插件。有点烦人,但在某些情况下可能是必要的。force_deactivation
(布尔值,可选) - 如果设置为 true
,一旦用户切换主题,插件将被停用。external_url
(字符串,可选) - 如果设置,插件的名称将链接到插件要求通知中的此地址。您可以通过三个选项让您的用户通过 TGM 插件激活安装插件:您可以从 WordPress 插件目录、外部源(例如您自己的服务器或 CDN)或您的主题文件夹(例如/my-theme/plugins/shortcodes.zip
)。
<?php $plugins = array( array( 'name' => 'BuddyPress', 'slug' => 'buddypress', 'required' => false, // this plugin is recommended ) ); ?>
<?php $plugins = array( array( 'name' => 'My Awesome Plugin', 'slug' => 'my-awesome-plugin', 'source' => 'http://files.my-website.com/my-awesome-plugin.zip', 'required' => true, // this plugin is required 'external_url' => 'http://my-website.com/introducing-my-awesome-plugin', // page of my plugin 'force_deactivation' => true, // deactivate this plugin when the user switches to another theme ) ); ?>
<?php $plugins = array( array( 'name' => 'My Super Sleek Slider', 'slug' => 'my-super-sleek-slider', 'source' => get_stylesheet_directory() . '/lib/plugins/my-super-sleek-slider.zip', // The "internal" source of the plugin. 'required' => true, // this plugin is required 'version' => '1.2', // the user must use version 1.2 (or higher) of this plugin 'force_activation' => false, // this plugin is going to stay activated unless the user switches to another theme ) ); ?>
注意到示例代码末尾带有两个参数的 tgmpa()
函数了吗?第二个参数是 $config
变量,它也恰好是一个数组,就像 $plugins
参数一样。顾名思义,您可以使用此数组配置 TGM 插件激活库。它还有自己的一组选项需要设置:
id
(字符串) - 您在主题中实现的 TGM 插件激活库的唯一 ID。这实际上非常重要:如果另一个插件也使用 TGM 插件激活,则不同的 ID 可以防止冲突。default_path
(string) - 主题内插件的默认绝对路径。设置此选项后,您可以使用 ZIP 文件的名称作为插件的 source
参数。menu
(字符串) - 插件安装页面的菜单项。has_notices
(boolean) - 如果设置为 true
,则会显示必需/推荐插件的管理员通知。dismissible
(boolean) - 如果设置为 true
,用户可以“忽略”通知。dismiss_msg
(string) - 如果 dismissable
选项设置为 false,则此消息将显示在管理通知上方。is_automatic
(boolean) - 如果设置为 true
,插件将在用户同意安装后激活。message
(string) - 在插件表之前显示的可选 HTML。strings
(array) - 另一个 array
包含要显示的消息。您也可以将它们设置为可翻译字符串。查看 example.php
文件以查看消息字符串的完整列表。<?php $config = array( 'id' => 'mytheme-tgmpa', // your unique TGMPA ID 'default_path' => get_stylesheet_directory() . '/lib/plugins/', // default absolute path 'menu' => 'mytheme-install-required-plugins', // menu slug 'has_notices' => true, // Show admin notices 'dismissable' => false, // the notices are NOT dismissable 'dismiss_msg' => 'I really, really need you to install these plugins, okay?', // this message will be output at top of nag 'is_automatic' => true, // automatically activate plugins after installation 'message' => '<!--Hey there.-->', // message to output right before the plugins table 'strings' => array(); // The array of message strings that TGM Plugin Activation uses ); ?>
正如您所看到的,通过 WordPress 主题提供功能并非不可能 - 您只需考虑用户从您的主题切换到另一个主题时的情况。 TGM 插件激活库提供了一种非常聪明的按书本操作的方法。
您觉得这个工具怎么样?您曾经使用过它,或者您打算将来使用它吗?请在下面发表评论,告诉我们您的想法。如果您喜欢这篇文章,请不要忘记与您的朋友分享!
The above is the detailed content of Leverage the power of the TGM plugin activation library in your theme. For more information, please follow other related articles on the PHP Chinese website!