增強您的主題:整合 Envato WordPress 工具包插件

王林
發布: 2023-09-04 12:13:01
原創
1182 人瀏覽過

增强您的主题:集成 Envato WordPress 工具包插件

身為 ThemeForest 中的 WordPress 作者,我們希望透過偶爾為客戶提供錯誤修復和主題增強功能來讓他們滿意。但我們面臨的一個關鍵問題是如何在有更新可供下載時通知我們的用戶。

在過去,我們每個人都必須在自己的主題更新通知程式實作中進行編碼。雖然現在有一個複選框可以啟用 Envato 市場中的項目更新通知,但用戶仍然必須針對每個項目打開它,並手動執行主題更新。

如果更新通知顯示在 WordPress 管理中心內不是更好嗎?並且可以在管理員中立即執行更新嗎?幸運的是,我們現在擁有 Envato WordPress 工具包外掛和工具包庫。

在本系列中,您將學習如何將這些工具包整合到您的主題中。


我們將在本系列中介紹什麼內容

在本教程中,我們將在我們的主題中實作 Envato WordPress 工具包外掛程式和函式庫。當我們的主題被啟動時,用戶將被要求安裝並啟動 Toolkit 外掛。

一旦外掛程式處於活動狀態,我們的主題將定期檢查更新,如果發現更新,則會在管理中顯示一則通知,引導使用者存取外掛程式以更新主題。

本教學分為兩部分:

  • 第 1 部分 - 整合 TGM 外掛啟動類,使使用我們的主題時需要 Envato WordPress Toolkit 外掛;和
  • 第 2 部分 - 在我們的主題中實作 Envato WordPress 工具包庫,以允許新的主題版本檢查和更新。

外掛程式和函式庫?

Envato WordPress 工具包有兩種形式,有不同的用途和目的。為了避免混淆這兩者,這裡有一個比較:

  • 工具包外掛程式 - 這是一個獨立的插件,任何 Envato 客戶都可以在其 WordPress 網站中安裝。啟動後,所有先前購買的主題以及主題更新都可以直接從管理員下載。
  • 工具包庫 - 作者可以在其 WordPress 主題中包含程式碼,使主題能夠使用 Envato Marketplace API 檢查主題版本更新並進行自我更新。

1.包含所需的檔案

我們首先需要在專案中包含一些文件。我們將把 Toolkit 外掛程式與我們的主題捆綁在一起,並使用 TGM 外掛程式啟動來安裝和啟動 Toolkit。

  1. 下載 TGM 外掛程式啟動並將主類別腳本放入主題中的 inc 資料夾中。路徑應為:mytheme/inc/class-tgm-plugin-activation.php
  2. #接下來,下載 Envato WordPress Toolkit 外掛程式 ZIP 檔案並將其放入主題中名為「plugins」的新資料夾中。路徑應為:mytheme/plugins/envato-wordpress-toolkit-master.zip

注意:您可以更改上述文件的位置以滿足您的需求。 或者,您可以從本文頂部的下載連結下載完整原始碼。


2.TGM鉤子函數

現在我們已經有了所需的文件,讓我們開始編碼。我們需要在 functions.php 中包含 TGM 外掛程式啟動類,並掛鉤到自訂 WordPress 操作。我們將在此處設置 TGM 的一些設置,並定義要包含的插件。

/**
 * Load the TGM Plugin Activator class to notify the user
 * to install the Envato WordPress Toolkit Plugin
 */
require_once( get_template_directory() . '/inc/class-tgm-plugin-activation.php' );
function tgmpa_register_toolkit() {

	// Code here

}
add_action( 'tgmpa_register', 'tgmpa_register_toolkit' );
登入後複製

3.指定Toolkit外掛

#接下來,我們配置包含 Toolkit 外掛所需的參數。在 tgmpa_register_toolkit 函數內,加入以下程式碼。如果您在第 1 步驟中指定了另一個外掛程式資料夾,請變更來源參數中的路徑。

// Specify the Envato Toolkit plugin
$plugins = array(
	array(
		'name' => 'Envato WordPress Toolkit',
		'slug' => 'envato-wordpress-toolkit-master',
		'source' => get_template_directory() . '/plugins/envato-wordpress-toolkit-master.zip',
		'required' => true,
		'version' => '1.5',
		'force_activation' => true,
		'force_deactivation' => false,
		'external_url' => '',
	),
);
登入後複製

您也可以透過在 $plugins 變數新增更多陣列來新增其他外掛程式。


4.配置TGM

然後設定 TGM 的選項。同樣在 tgmpa_register_toolkit 函數中,在上一步下方加入以下程式碼來設定 TGM。我不會深入探討各個設定的具體作用。如果您想了解有關這些設定的更多信息,TGM 插件啟動網站可以很好地解釋每一個細節。

// i18n text domain used for translation purposes
$theme_text_domain = 'default';

// Configuration of TGM
$config = array(
	'domain'       	   => $theme_text_domain,
	'default_path' 	   => '',
	'parent_menu_slug' => 'admin.php',
	'parent_url_slug'  => 'admin.php',
	'menu'         	   => 'install-required-plugins',
	'has_notices'      => true,
	'is_automatic'     => true,
	'message' 		   => '',
	'strings'      	   => array(
		'page_title'                      => __( 'Install Required Plugins', $theme_text_domain ),
		'menu_title'                      => __( 'Install Plugins', $theme_text_domain ),
		'installing'                      => __( 'Installing Plugin: %s', $theme_text_domain ),
		'oops'                            => __( 'Something went wrong with the plugin API.', $theme_text_domain ),
		'notice_can_install_required'     => _n_noop( 'This theme requires the following plugin: %1$s.', 'This theme requires the following plugins: %1$s.' ),
		'notice_can_install_recommended'  => _n_noop( 'This theme recommends the following plugin: %1$s.', 'This theme recommends the following plugins: %1$s.' ),
		'notice_cannot_install'  		  => _n_noop( '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.', 'Sorry, but you do not have the correct permissions to install the %s plugins. Contact the administrator of this site for help on getting the plugins installed.' ),
		'notice_can_activate_required'    => _n_noop( 'The following required plugin is currently inactive: %1$s.', 'The following required plugins are currently inactive: %1$s.' ),
		'notice_can_activate_recommended' => _n_noop( 'The following recommended plugin is currently inactive: %1$s.', 'The following recommended plugins are currently inactive: %1$s.' ),
		'notice_cannot_activate' 		  => _n_noop( '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.', 'Sorry, but you do not have the correct permissions to activate the %s plugins. Contact the administrator of this site for help on getting the plugins activated.' ),
		'notice_ask_to_update' 			  => _n_noop( 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.' ),
		'notice_cannot_update' 			  => _n_noop( 'Sorry, but you do not have the correct permissions to update the %s plugin. Contact the administrator of this site for help on getting the plugin updated.', 'Sorry, but you do not have the correct permissions to update the %s plugins. Contact the administrator of this site for help on getting the plugins updated.' ),
		'install_link' 					  => _n_noop( 'Begin installing plugin', 'Begin installing plugins' ),
		'activate_link' 				  => _n_noop( 'Activate installed plugin', 'Activate installed plugins' ),
		'return'                          => __( 'Return to Required Plugins Installer', $theme_text_domain ),
		'plugin_activated'                => __( 'Plugin activated successfully.', $theme_text_domain ),
		'complete' 						  => __( 'All plugins installed and activated successfully. %s', $theme_text_domain ),
		'nag_type'						  => 'updated'
	)
);
登入後複製

$theme_text_domain 變數變更為您正在使用的文字網域,或將其保留為 default


5.啟動TGM

最後,讓我們在 tgmpa_register_toolkit 函數結束之前初始化 TGM。

tgmpa( $plugins, $config );
登入後複製

現在儲存您的functions.php


#嘗試一下

嘗試啟動您的主題。如果您尚未安裝或啟動 Envato WordPress Toolkit 外掛,那麼您應該會看到與此類似的通知:

增强您的主题:集成 Envato WordPress 工具包插件


#結論

從我們現在所掌握的情況來看,我們實際上可以停止該系列,您的用戶將能夠從管理員內部更新主題;但是,用戶只有在 Toolkit 管理面板中才能看到更新。

本教學的第 2 部分將教您如何整合 Envato WordPress 工具包庫,以及如何在 ThemeForest 中出現主題更新時顯示管理通知。

#

以上是增強您的主題:整合 Envato WordPress 工具包插件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!