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

熱AI工具

Undress AI Tool
免費(fèi)脫衣圖片

Undresser.AI Undress
人工智慧驅(qū)動(dòng)的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級(jí)程式碼編輯軟體(SublimeText3)

要回滾WordPress版本,可使用插件或手動(dòng)替換核心文件,並禁用自動(dòng)更新。 1.使用WPDowngrade等插件輸入目標(biāo)版本號(hào)即可自動(dòng)下載替換;2.手動(dòng)下載舊版WordPress並通過FTP替換wp-includes、wp-admin等文件但保留wp-config.php和wp-content;3.在wp-config.php中添加代碼或使用過濾器禁用核心自動(dòng)更新以防止再次升級(jí)。操作前務(wù)必備份網(wǎng)站和數(shù)據(jù)庫,確保安全可靠。長期建議保持最新版以保障安全性與功能支持。

在WordPress中創(chuàng)建自定義短代碼的步驟如下:1.通過functions.php文件或自定義插件編寫PHP函數(shù);2.使用add_shortcode()將函數(shù)綁定到短代碼標(biāo)籤;3.在函數(shù)中處理參數(shù)並返回輸出內(nèi)容。例如,創(chuàng)建按鈕短代碼時(shí)可定義顏色和鏈接參數(shù),實(shí)現(xiàn)靈活配置。使用時(shí)可在編輯器中插入類似[buttoncolor="red"url="https://example.com"]點(diǎn)擊這裡[/button]的標(biāo)籤,並可通過do_shortcode()在模

WordPress導(dǎo)致服務(wù)器CPU使用率飆升的主要原因包括插件問題、數(shù)據(jù)庫查詢效率低、主題代碼質(zhì)量差或流量激增。 1.首先通過top、htop或控制面板工具確認(rèn)是否為WordPress引起的高負(fù)載;2.進(jìn)入故障排查模式逐步啟用插件排查性能瓶頸,使用QueryMonitor分析插件執(zhí)行情況並刪除或替換低效插件;3.安裝緩存插件、清理冗餘數(shù)據(jù)、分析慢查詢?nèi)照I以優(yōu)化數(shù)據(jù)庫;4.檢查主題是否存在過度加載內(nèi)容、複雜查詢或缺乏緩存機(jī)制等問題,建議用標(biāo)準(zhǔn)主題測(cè)試對(duì)比並優(yōu)化代碼邏輯。按照上述步驟逐一排查可定位並解

優(yōu)化WordPress站點(diǎn)不依賴插件的方法包括:1.使用輕量級(jí)主題,如Astra或GeneratePress,避免功能堆砌的主題;2.手動(dòng)壓縮和合併CSS、JS文件,減少HTTP請(qǐng)求;3.上傳前優(yōu)化圖片,使用WebP格式並控製文件大?。?.配置.htaccess啟用瀏覽器緩存,並接入CDN提升靜態(tài)資源加載速度;5.限製文章修訂版本並定期清理數(shù)據(jù)庫冗餘數(shù)據(jù)。

MinifyingJavaScript文件可通過刪除空白、註釋和無用代碼來提升WordPress網(wǎng)站加載速度。 1.使用支持合併壓縮的緩存插件如W3TotalCache,在“Minify”選項(xiàng)中啟用並選擇壓縮模式;2.使用專用壓縮插件如FastVelocityMinify,提供更精細(xì)控制;3.手動(dòng)壓縮JS文件並通過FTP上傳,適用於熟悉開發(fā)工具的用戶。注意部分主題或插件腳本可能與壓縮功能衝突,啟用後需徹底測(cè)試網(wǎng)站功能。

TransientsAPI是WordPress中用於臨時(shí)存儲(chǔ)可自動(dòng)過期數(shù)據(jù)的內(nèi)置工具,其核心函數(shù)為set_transient、get_transient和delete_transient。相比OptionsAPI,transients支持設(shè)置生存時(shí)間(TTL),適合緩存API請(qǐng)求結(jié)果、複雜計(jì)算數(shù)據(jù)等場(chǎng)景。使用時(shí)需注意key命名唯一性與命名空間、緩存“懶刪除”機(jī)制及對(duì)象緩存環(huán)境下可能不持久的問題。典型應(yīng)用場(chǎng)景包括減少外部請(qǐng)求頻率、控制代碼執(zhí)行節(jié)奏和提升頁面加載性能。

對(duì)象緩存可輔助持久存儲(chǔ),適用於高訪問低更新、可容忍短暫丟失的數(shù)據(jù)。 1.適合用緩存“持久化”的數(shù)據(jù)包括用戶配置、熱門商品信息等,能從數(shù)據(jù)庫恢復(fù)但使用緩存可加速訪問。 2.選擇Redis等支持持久化的緩存後端,啟用RDB或AOF模式,並配置合理過期策略,但不能替代主數(shù)據(jù)庫。 3.設(shè)置長TTL或永不過期鍵,採用清晰鍵名結(jié)構(gòu)如user:1001:profile,修改數(shù)據(jù)時(shí)同步更新緩存。 4.可結(jié)合本地與分佈式緩存,本地存小數(shù)據(jù)、Redis存大數(shù)據(jù)並用於重啟後恢復(fù),同時(shí)注意一致性與資源佔(zhàn)用問題。

防止評(píng)論垃圾信息最有效的方式是通過程序化手段自動(dòng)識(shí)別並攔截。 1.使用驗(yàn)證碼機(jī)制(如GooglereCAPTCHA或hCaptcha)可有效區(qū)分人類與機(jī)器人,尤其適合公眾網(wǎng)站;2.設(shè)置隱藏字段(Honeypot技術(shù)),利用機(jī)器人自動(dòng)填寫特性識(shí)別垃圾評(píng)論,不影響用戶體驗(yàn);3.檢查評(píng)論內(nèi)容關(guān)鍵詞黑名單,通過敏感詞匹配過濾垃圾信息,需注意避免誤判;4.判斷評(píng)論頻率與來源IP,限制單位時(shí)間內(nèi)的提交次數(shù)並建立黑名單;5.使用第三方反垃圾服務(wù)(如Akismet、Cloudflare)提升識(shí)別準(zhǔn)確性??筛鶕?jù)網(wǎng)站
