作為 ThemeForest 中的 WordPress 作者,我們希望通過偶爾為客戶提供錯誤修復(fù)和主題增強(qiáng)功能來讓他們滿意。但我們面臨的一個關(guān)鍵問題是如何在有更新可供下載時通知我們的用戶。
在過去,我們每個人都必須在自己的主題更新通知程序?qū)崿F(xiàn)中進(jìn)行編碼。雖然現(xiàn)在有一個復(fù)選框可以啟用 Envato 市場中的項(xiàng)目更新通知,但用戶仍然必須針對每個項(xiàng)目打開它,并手動執(zhí)行主題更新。
如果更新通知顯示在 WordPress 管理中心內(nèi)不是更好嗎?并且可以在管理員中立即執(zhí)行更新嗎?幸運(yùn)的是,我們現(xiàn)在擁有 Envato WordPress 工具包插件和工具包庫。
在本系列中,您將學(xué)習(xí)如何將這些工具包集成到您的主題中。
我們將在本系列中介紹什么內(nèi)容
在本教程中,我們將在我們的主題中實(shí)現(xiàn) Envato WordPress 工具包插件和庫。當(dāng)我們的主題被激活時,用戶將被要求安裝并激活 Toolkit 插件。
一旦插件處于活動狀態(tài),我們的主題將定期檢查更新,如果發(fā)現(xiàn)更新,則會在管理中顯示一條通知,引導(dǎo)用戶訪問插件以更新主題。
本教程分為兩部分:
- 第 1 部分 – 集成 TGM 插件激活類,使使用我們的主題時需要 Envato WordPress Toolkit 插件;和
- 第 2 部分 – 在我們的主題中實(shí)現(xiàn) Envato WordPress 工具包庫,以允許新的主題版本檢查和更新。
插件和庫?
Envato WordPress 工具包有兩種形式,具有不同的用途和目的。為了避免混淆這兩者,這里有一個比較:
- 工具包插件 – 這是一個獨(dú)立的插件,任何 Envato 客戶都可以在其 WordPress 網(wǎng)站中安裝。激活后,所有以前購買的主題以及主題更新都可以直接從管理員下載。
- 工具包庫 – 作者可以在其 WordPress 主題中包含代碼,使主題能夠使用 Envato Marketplace API 檢查主題版本更新并進(jìn)行自我更新。
1.包括所需的文件
我們首先需要在項(xiàng)目中包含一些文件。我們將把 Toolkit 插件與我們的主題捆綁在一起,并使用 TGM 插件激活來安裝和激活 Toolkit。
- 下載 TGM 插件激活并將主類腳本放入主題中的 inc 文件夾中。路徑應(yīng)為:mytheme/inc/class-tgm-plugin-activation.php
- 接下來,下載 Envato WordPress Toolkit 插件 ZIP 文件并將其放入主題中名為“plugins”的新文件夾中。路徑應(yīng)為:mytheme/plugins/envato-wordpress-toolkit-master.zip
注意:您可以更改上述文件的位置以滿足您的需要。 或者,您可以從本文頂部的下載鏈接下載完整源代碼。
2.TGM鉤子函數(shù)
現(xiàn)在我們已經(jīng)有了所需的文件,讓我們開始編碼。我們需要在 functions.php 中包含 TGM 插件激活類,并掛鉤到自定義 WordPress 操作。我們將在此處設(shè)置 TGM 的一些設(shè)置,并定義要包含的插件。
/** * 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 插件所需的參數(shù)。在 tgmpa_register_toolkit 函數(shù)內(nèi),添加以下代碼。如果您在第 1 步中指定了另一個插件文件夾,請更改源參數(shù)中的路徑。
// 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 變量添加更多數(shù)組來添加其他插件。
4.配置TGM
然后設(shè)置 TGM 的選項(xiàng)。同樣在 tgmpa_register_toolkit 函數(shù)中,在上一步下方添加以下代碼來配置 TGM。我不會深入探討各個設(shè)置的具體作用。如果您想了解有關(guān)這些設(shè)置的更多信息,TGM 插件激活網(wǎng)站可以很好地解釋每一個細(xì)節(jié)。
// 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 變量更改為您正在使用的文本域,或?qū)⑵浔A魹?default。
5.啟動TGM
最后,讓我們在 tgmpa_register_toolkit 函數(shù)結(jié)束之前初始化 TGM。
tgmpa( $plugins, $config );
現(xiàn)在保存您的functions.php
嘗試一下
嘗試激活您的主題。如果您尚未安裝或激活 Envato WordPress Toolkit 插件,那么您應(yīng)該會看到與此類似的通知:
結(jié)論
從我們現(xiàn)在所掌握的情況來看,我們實(shí)際上可以停止該系列,您的用戶將能夠從管理員內(nèi)部更新主題;但是,用戶只有在 Toolkit 管理面板中才能看到更新。
本教程的第 2 部分將教您如何集成 Envato WordPress 工具包庫,以及如何在 ThemeForest 中出現(xiàn)主題更新時顯示管理通知。