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.
Theme function: Invasion of plug-in territory
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.
Introduction to TGM plug-in activation library
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!
Install TGM plug-in activation
Installing TGM plug-in activation is very simple. Just follow these steps:
- Download the TGM plugin activation library from the Downloads section of the page.
- Open the zip file and extract
class-tgm-plugin-activation.php
to your theme folder (anywhere you like). - Open the theme's
functions.php
file and use therequire_once()
function to request the class file (once) in the theme. - Create a function to configure TGM plugin activation and hook it to
tgmpa_register
via theadd_action()
function. - Finish!
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.
Activate and install the plug-in through TGM plug-in
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
)。
需要 WordPress.org 的插件
<?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 ) ); ?>
配置 TGM 插件激活
注意到示例代码末尾带有两个参数的 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!

Can learn WordPress within three days. 1. Master basic knowledge, such as themes, plug-ins, etc. 2. Understand the core functions, including installation and working principles. 3. Learn basic and advanced usage through examples. 4. Understand debugging techniques and performance optimization suggestions.

WordPress is a Content Management System (CMS). It provides content management, user management, themes and plug-in capabilities to support the creation and management of website content. Its working principle includes database management, template systems and plug-in architecture, suitable for a variety of needs from blogs to corporate websites.

WordPressisgoodforvirtuallyanywebprojectduetoitsversatilityasaCMS.Itexcelsin:1)user-friendliness,allowingeasywebsitesetup;2)flexibilityandcustomizationwithnumerousthemesandplugins;3)SEOoptimization;and4)strongcommunitysupport,thoughusersmustmanageper

Wix is suitable for users who have no programming experience, and WordPress is suitable for users who want more control and expansion capabilities. 1) Wix provides drag-and-drop editors and rich templates, making it easy to quickly build a website. 2) As an open source CMS, WordPress has a huge community and plug-in ecosystem, supporting in-depth customization and expansion.

WordPress itself is free, but it costs extra to use: 1. WordPress.com offers a package ranging from free to paid, with prices ranging from a few dollars per month to dozens of dollars; 2. WordPress.org requires purchasing a domain name (10-20 US dollars per year) and hosting services (5-50 US dollars per month); 3. Most plug-ins and themes are free, and the paid price ranges from tens to hundreds of dollars; by choosing the right hosting service, using plug-ins and themes reasonably, and regularly maintaining and optimizing, the cost of WordPress can be effectively controlled and optimized.

The core version of WordPress is free, but other fees may be incurred during use. 1. Domain names and hosting services require payment. 2. Advanced themes and plug-ins may be charged. 3. Professional services and advanced features may be charged.

WordPress is easy for beginners to get started. 1. After logging into the background, the user interface is intuitive and the simple dashboard provides all the necessary function links. 2. Basic operations include creating and editing content. The WYSIWYG editor simplifies content creation. 3. Beginners can expand website functions through plug-ins and themes, and the learning curve exists but can be mastered through practice.

People choose to use WordPress because of its power and flexibility. 1) WordPress is an open source CMS with strong ease of use and scalability, suitable for various website needs. 2) It has rich themes and plugins, a huge ecosystem and strong community support. 3) The working principle of WordPress is based on themes, plug-ins and core functions, and uses PHP and MySQL to process data, and supports performance optimization.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools