国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Theme function: Invasion of plug-in territory
Introduction to TGM plug-in activation library
Install TGM plug-in activation
Activate and install the plug-in through TGM plug-in
需要 WordPress.org 的插件
從外部源請求插件
從主題目錄中獲取插件
配置 TGM 插件激活
總結(jié)一切
Home CMS Tutorial WordPress Leverage the power of the TGM plugin activation library in your theme

Leverage the power of the TGM plugin activation library in your theme

Sep 01, 2023 pm 10:49 PM

在您的主題中利用 TGM 插件激活庫的強大功能

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 the require_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 the add_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 (布爾值,必需) - 如果設(shè)置為 true,您的主題將“需要”該插件。如果false,主題將“推薦”它。
  • source (字符串,有時需要)- 插件的源。如果是 WordPress.org 插件,則不應(yīng)使用此參數(shù);否則,這是必需的。
  • version (字符串,可選) - 插件所需的最低版本。例如;如果主題用戶已經(jīng)安裝了所需的插件,但沒有達到您指定的最低版本號,TGM 插件激活會警告用戶進行更新。
  • force_activation (布爾值,可選) - 如果設(shè)置為 true,當(dāng)您的主題處于活動狀態(tài)時,用戶將無法停用插件。有點煩人,但在某些情況下可能是必要的。
  • force_deactivation (布爾值,可選) - 如果設(shè)置為 true,一旦用戶切換主題,插件將被停用。
  • external_url (字符串,可選) - 如果設(shè)置,插件的名稱將鏈接到插件要求通知中的此地址。

您可以通過三個選項讓您的用戶通過 TGM 插件激活安裝插件:您可以從 WordPress 插件目錄、外部源(例如您自己的服務(wù)器或 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 插件激活

注意到示例代碼末尾帶有兩個參數(shù)的 tgmpa() 函數(shù)了嗎?第二個參數(shù)是 $config 變量,它也恰好是一個數(shù)組,就像 $plugins 參數(shù)一樣。顧名思義,您可以使用此數(shù)組配置 TGM 插件激活庫。它還有自己的一組選項需要設(shè)置:

  • id(字符串) - 您在主題中實現(xiàn)的 TGM 插件激活庫的唯一 ID。這實際上非常重要:如果另一個插件也使用 TGM 插件激活,則不同的 ID 可以防止沖突。
  • default_path (string) - 主題內(nèi)插件的默認絕對路徑。設(shè)置此選項后,您可以使用 ZIP 文件的名稱作為插件的 source 參數(shù)。
  • menu (字符串) - 插件安裝頁面的菜單項。
  • has_notices (boolean) - 如果設(shè)置為 true,則會顯示必需/推薦插件的管理員通知。
  • dismissible (boolean) - 如果設(shè)置為 true,用戶可以“忽略”通知。
  • dismiss_msg (string) - 如果 dismissable 選項設(shè)置為 false,則此消息將顯示在管理通知上方。
  • is_automatic (boolean) - 如果設(shè)置為 true,插件將在用戶同意安裝后激活。
  • message (string) - 在插件表之前顯示的可選 HTML。
  • strings (array) - 另一個 array 包含要顯示的消息。您也可以將它們設(shè)置為可翻譯字符串。查看 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
);

?>

總結(jié)一切

正如您所看到的,通過 WordPress 主題提供功能并非不可能 - 您只需考慮用戶從您的主題切換到另一個主題時的情況。 TGM 插件激活庫提供了一種非常聰明的按書本操作的方法。

您覺得這個工具怎么樣?您曾經(jīng)使用過它,或者您打算將來使用它嗎?請在下面發(fā)表評論,告訴我們您的想法。如果您喜歡這篇文章,請不要忘記與您的朋友分享!

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use the WordPress testing environment How to use the WordPress testing environment Jun 24, 2025 pm 05:13 PM

Use WordPress testing environments to ensure the security and compatibility of new features, plug-ins or themes before they are officially launched, and avoid affecting real websites. The steps to build a test environment include: downloading and installing local server software (such as LocalWP, XAMPP), creating a site, setting up a database and administrator account, installing themes and plug-ins for testing; the method of copying a formal website to a test environment is to export the site through the plug-in, import the test environment and replace the domain name; when using it, you should pay attention to not using real user data, regularly cleaning useless data, backing up the test status, resetting the environment in time, and unifying the team configuration to reduce differences.

How to use Git with WordPress How to use Git with WordPress Jun 26, 2025 am 12:23 AM

When managing WordPress projects with Git, you should only include themes, custom plugins, and configuration files in version control; set up .gitignore files to ignore upload directories, caches, and sensitive configurations; use webhooks or CI tools to achieve automatic deployment and pay attention to database processing; use two-branch policies (main/develop) for collaborative development. Doing so can avoid conflicts, ensure security, and improve collaboration and deployment efficiency.

How to create a simple Gutenberg block How to create a simple Gutenberg block Jun 28, 2025 am 12:13 AM

The key to creating a Gutenberg block is to understand its basic structure and correctly connect front and back end resources. 1. Prepare the development environment: install local WordPress, Node.js and @wordpress/scripts; 2. Use PHP to register blocks and define the editing and display logic of blocks with JavaScript; 3. Build JS files through npm to make changes take effect; 4. Check whether the path and icons are correct when encountering problems or use real-time listening to build to avoid repeated manual compilation. Following these steps, a simple Gutenberg block can be implemented step by step.

How to set up redirects in WordPress htaccess How to set up redirects in WordPress htaccess Jun 25, 2025 am 12:19 AM

TosetupredirectsinWordPressusingthe.htaccessfile,locatethefileinyoursite’srootdirectoryandaddredirectrulesabovethe#BEGINWordPresssection.Forbasic301redirects,usetheformatRedirect301/old-pagehttps://example.com/new-page.Forpattern-basedredirects,enabl

How to flush rewrite rules programmatically How to flush rewrite rules programmatically Jun 27, 2025 am 12:21 AM

In WordPress, when adding a custom article type or modifying the fixed link structure, you need to manually refresh the rewrite rules. At this time, you can call the flush_rewrite_rules() function through the code to implement it. 1. This function can be added to the theme or plug-in activation hook to automatically refresh; 2. Execute only once when necessary, such as adding CPT, taxonomy or modifying the link structure; 3. Avoid frequent calls to avoid affecting performance; 4. In a multi-site environment, refresh each site separately as appropriate; 5. Some hosting environments may restrict the storage of rules. In addition, clicking Save to access the "Settings>Pinned Links" page can also trigger refresh, suitable for non-automated scenarios.

How to send email from WordPress using SMTP How to send email from WordPress using SMTP Jun 27, 2025 am 12:30 AM

UsingSMTPforWordPressemailsimprovesdeliverabilityandreliabilitycomparedtothedefaultPHPmail()function.1.SMTPauthenticateswithyouremailserver,reducingspamplacement.2.SomehostsdisablePHPmail(),makingSMTPnecessary.3.SetupiseasywithpluginslikeWPMailSMTPby

How to make a WordPress theme responsive How to make a WordPress theme responsive Jun 28, 2025 am 12:14 AM

To implement responsive WordPress theme design, first, use HTML5 and mobile-first Meta tags, add viewport settings in header.php to ensure that the mobile terminal is displayed correctly, and organize the layout with HTML5 structure tags; second, use CSS media query to achieve style adaptation under different screen widths, write styles according to the mobile-first principle, and commonly used breakpoints include 480px, 768px and 1024px; third, elastically process pictures and layouts, set max-width:100% for the picture and use Flexbox or Grid layout instead of fixed width; finally, fully test through browser developer tools and real devices, optimize loading performance, and ensure response

How to integrate third-party APIs with WordPress How to integrate third-party APIs with WordPress Jun 29, 2025 am 12:03 AM

Tointegratethird-partyAPIsintoWordPress,followthesesteps:1.SelectasuitableAPIandobtaincredentialslikeAPIkeysorOAuthtokensbyregisteringandkeepingthemsecure.2.Choosebetweenpluginsforsimplicityorcustomcodeusingfunctionslikewp_remote_get()forflexibility.

See all articles