>以對(duì)象為導(dǎo)向的代碼無處不在,WordPress也不例外。
WordPress的核心構(gòu)建在用于控制所有內(nèi)容的一系列對(duì)象/類上 在本教程中,我們將研究面向?qū)ο蟮脑O(shè)計(jì),以及如何在為WordPress開發(fā)時(shí)如何在實(shí)際應(yīng)用中使用這些設(shè)計(jì),包括:
準(zhǔn)確地了解哪種面向?qū)ο蟮脑O(shè)計(jì)是。
>- 討論為什么我們應(yīng)該使用面向?qū)ο蟮脑O(shè)計(jì)。
- >檢查一個(gè)真實(shí)的示例以展示如何工作。
- >注意1:另請(qǐng)注意,該教程主要概述了以WordPress為中心的概念,因此,如果您是面向?qū)ο蟮脑O(shè)計(jì)的全部新概念,則可能應(yīng)該通過PHP參考指南快速概述OO。
- >注2:如果您渴望立即下載所有內(nèi)容,請(qǐng)隨時(shí)從我的github存儲(chǔ)庫(kù)下載源并與教程一起關(guān)注。
>
鑰匙要點(diǎn)WordPress組中的面向?qū)ο蟮脑O(shè)計(jì)(OOD)相關(guān)的功能和信息中的信息,增強(qiáng)代碼組織和維護(hù)。
> OOD提供了諸如通過封裝,通過繼承的可伸縮性和模塊化的更好的組織提供好處,以便于代碼重復(fù)使用和管理。
>教程提供了在WordPress中創(chuàng)建自定義事件展示的一個(gè)實(shí)踐示例,證明了OOD原理的實(shí)現(xiàn)。 示例中的>關(guān)鍵步驟包括設(shè)置目錄結(jié)構(gòu),創(chuàng)建類文件,并實(shí)現(xiàn)用于添加和顯示自定義內(nèi)容類型和元框的功能。
該指南強(qiáng)調(diào)了OOD比程序編程的優(yōu)勢(shì),強(qiáng)調(diào)組織,可伸縮性和模塊化。- >教程以行動(dòng)呼吁從GitHub下載源代碼,鼓勵(lì)讀者在其WordPress開發(fā)項(xiàng)目中應(yīng)用所示的OOD概念。
- 無論如何,什么是面向?qū)ο蟮脑O(shè)計(jì)? >
- >對(duì)象方向設(shè)計(jì)(OO設(shè)計(jì))是解決問題的不同方法,與傳統(tǒng)的程序方法分開。 >使用對(duì)象設(shè)計(jì)的設(shè)計(jì),您可以創(chuàng)建一個(gè)結(jié)構(gòu)(類),該結(jié)構(gòu)(類)將將所有相關(guān)功能(方法)和信息(屬性)組合在一起以解決問題。
- > 這與您創(chuàng)建函數(shù)和變量以解決問題的過程開發(fā)不同,但是,這些元素可以分離出多個(gè)文件,重新播出并經(jīng)常與之相關(guān)。 簡(jiǎn)而言之,對(duì)象的設(shè)計(jì)是將相關(guān)功能匯總到一個(gè)位置,以便可以從單個(gè)位置維護(hù),擴(kuò)展和使用。
- 創(chuàng)建一種自定義內(nèi)容類型來管理這些“事件”。
- 添加其他元框來處理其他信息處理。 >
- 保存其他元信息。
- 顯示我們的“事件”的元信息。
- > event_showcase_public_styles.css
- > event_showcase_public_scripts.js
- 您應(yīng)該有類似于以下類似的東西:
- >以后您需要下載并將jQuery Time Picker腳本和樣式放置在此目錄中。不用擔(dān)心這個(gè)。
- __ construct() - 我們所有的鉤子,過濾器和初始化代碼都將進(jìn)入此處。 >
- set_directory_value() - 定義我們的目錄屬性,該屬性將使我們輕松地插入我們?cè)贑SS或JS目錄中放置的文件。 >
- > enqueue_admin_scripts_and_styles() - 加載管理腳本和樣式。
- add_content_type() - 定義我們正在創(chuàng)建的新內(nèi)容類型。
- > add_meta_boxes_for_content_type() - 為我們的新內(nèi)容類型添加元框。
- display_function_for_content_type_meta_box - 構(gòu)建內(nèi)容類型的后端管理接口,因此我們可以保存額外的信息。
- > > save_custom_content_type($ post_id) - 處理自定義內(nèi)容類型的保存(和我們的元信息)。
- display_additional_meta_data() - 在前端顯示保存的元信息。
- >讓我們現(xiàn)在瀏覽一下,更新這些元素并概述代碼 >
為什么對(duì)象方向是個(gè)好主意?
>由于程序代碼運(yùn)行良好,因此您可能想知道以對(duì)象為導(dǎo)向的方法開發(fā)的意義是什么?
>雖然程序設(shè)計(jì)沒有“錯(cuò)誤”,但以更面向?qū)ο蟮臉邮介_發(fā)有幾個(gè)好處。
>組織>使用對(duì)象導(dǎo)向方法時(shí),通常會(huì)更好地組織代碼。創(chuàng)建類意味著解決問題的功能都在文件中(與程序開發(fā)不同,您可能擁有函數(shù)和變量到處都有)。
將所有代碼都放在一個(gè)地方可以幫助您保持井井有條,并協(xié)助其他可能會(huì)出現(xiàn)并需要了解您的系統(tǒng)的開發(fā)人員。
>
>沒有什么比撿起一個(gè)項(xiàng)目并不得不四處尋找的函數(shù)和變量更糟糕的是,這些功能和變量用于創(chuàng)建網(wǎng)站的各種元素(由于未知原因而始終分布在多個(gè)文件上)。>
可伸縮性>以對(duì)象為導(dǎo)向的方法求解問題時(shí),您可以創(chuàng)建類。
>
可以通過使用繼承擴(kuò)展這些類來擴(kuò)展或縮放這些類。繼承將使您基于另一個(gè)類創(chuàng)建一個(gè)類,該類可訪問其所有屬性和方法。>您可以擴(kuò)展一類以執(zhí)行其他功能,甚至可以覆蓋其方法和屬性以迎合這種情況。
>使用程序代碼,您要么需要重構(gòu)代碼的一部分,要么完全創(chuàng)建新代碼以處理其他情況。模塊化
對(duì)象設(shè)計(jì)的目的是封裝所有功能,以將情況求解到類中。
>由于解決問題的所有代碼都在類內(nèi)部,并且該類通常在其自己的PHP文件中,因此您可以將整個(gè)文件放在另一個(gè)位置,因此它本質(zhì)上是模塊化的。>這樣的好處是,如果您編寫足夠好的課程,則應(yīng)該可以在以后重復(fù)使用它們,并且只需執(zhí)行輕微的自定義。
>>您可以復(fù)制和粘貼程序代碼,但通常情況不太井井有條,您可能必須瀏覽幾個(gè)文件以確保您擁有所需的功能。
在這種情況下,我們的客戶希望在其網(wǎng)站上可以創(chuàng)建和管理事件的位置。這些活動(dòng)將用于促進(jìn)即將到來的瑣事之夜,賓果游戲和其他有趣的活動(dòng)。
>第一步是分解所需的功能,以便我們可以計(jì)劃解決方案(在這里進(jìn)行良好的計(jì)劃將有助于我們以對(duì)象為導(dǎo)向的方法進(jìn)行)。在我們的情況下,我們可以看到以下一組任務(wù):我們可以創(chuàng)建一個(gè)插件,也可以編輯子主題以提供此功能。
因?yàn)槲覀兿M袃?nèi)容都可以容易地管理和模塊化,所以讓我們從“孩子主題”內(nèi)部設(shè)置文件夾結(jié)構(gòu)開始。
>組織所有內(nèi)容的簡(jiǎn)便方法是創(chuàng)建孩子主題中的包含目錄。該目錄將包含您的主題包含/實(shí)施的其他代碼或功能(您可以隨意調(diào)用此功能,目的是在主題中擁有一個(gè)單一目錄)。
在此目錄中,我們應(yīng)該制作另一個(gè)文件夾,這次稱為event_showcase。在此文件夾中,我們將在此情況下放置所有適用的文件。 我們還需要一個(gè)JS和一個(gè)CSS文件夾。
>將功能組織到文件夾中將意味著您可以更輕松地查看哪些模塊 /功能您的主題工具。例如,您可能需要為主題實(shí)現(xiàn)另一組功能,您可以添加一個(gè)新文件夾,從邏輯上講,所有內(nèi)容都整齊地組織了。
>讓我們?yōu)槲覀兊氖纠齽?chuàng)建文件,然后將它們移至正確的目錄>
> event_showcase_class.php
> event_showcase_admin_styles.css
- > event_showcase_admin_scripts.js
>將文件加載到WordPress
中
>打開您的孩子主題的函數(shù)。php,并將以下代碼放在頂部或底部
>>這將加載主類文件,該文件將概述我們的功能。在此類課程中,我們將處理腳本和樣式的重新設(shè)計(jì)以及其他元素。
創(chuàng)建類概述
>我們的類文件稱為event_showcase_class.php將包含用于我們的事件系統(tǒng)的大多數(shù)功能;因此,最好繪制出我們需要進(jìn)行這項(xiàng)工作需要的方法和屬性的基本概述。
><span>//Theme functions for child theme </span> <span>//load our event showcase class </span><span>$event_showcase = get_stylesheet_directory() . '/includes/event_showcase/event_showcase_class.php'; </span><span>include($event_showcase);</span>>將以下代碼復(fù)制到您的類文件中。
<span>//Theme functions for child theme </span> <span>//load our event showcase class </span><span>$event_showcase = get_stylesheet_directory() . '/includes/event_showcase/event_showcase_class.php'; </span><span>include($event_showcase);</span>
>讓我們通過方法和屬性運(yùn)行,以便我們可以理解過程流程:
- >變量 - 我們聲明一些變量以使類更模塊化(因此我們可以復(fù)制此文件并更改一些屬性,并且類將處理其他更改)。
>我們?cè)陬惖捻敳柯暶髁艘恍⑼ㄟ^我們的方法引用的屬性。這些目的是使它們更容易重新置換此類并在將來更改其功能。
__ construct()
<span>/* </span><span> * Event Showcase </span><span> * Creates an 'event' content type to showcase upcoming functions and information </span><span> * Uses hooks and filters inside your theme to output relevant information </span><span> */ </span> <span>class event_showcase{ </span> <span>//variables </span> <span>private $directory = ''; </span> <span>private $singular_name = 'event'; </span> <span>private $plural_name = 'events'; </span> <span>private $content_type_name = 'event_showcase'; </span> <span>//magic function, called on creation </span> <span>public function __construct(){} </span> <span>//sets the directory (path) so that we can use this for our enqueuing </span> <span>public function set_directory_value(){} </span> <span>//check if we need to flush rewrite rules </span> <span>public function check_flush_rewrite_rules(){} </span> <span>//enqueue public scripts and styles </span> <span>public function enqueue_public_scripts_and_styles(){} </span> <span>//enqueue admin scripts and styles </span> <span>public function enqueue_admin_scripts_and_styles(){} </span> <span>//adding our new content type </span> <span>public function add_content_type(){} </span> <span>//adding meta box to save additional meta data for the content type </span> <span>public function add_meta_boxes_for_content_type(){} </span> <span>//displays the visual output of the meta box in admin (where we will save our meta data) </span> <span>public function display_function_for_content_type_meta_box($post){} </span> <span>//when saving the custom content type, save additional meta data </span> <span>public function save_custom_content_type($post_id){} </span> <span>//display additional meta information for the content type </span> <span>//@hooked using 'display_additional_meta_data' in theme </span> <span>function display_additional_meta_data(){} </span> <span>} </span> <span>//create new object </span> <span>$event_showcase = new event_showcase;</span>我們的__ -construct函數(shù)是一個(gè)魔術(shù)函數(shù)。這些功能是類的特殊PHP5功能,可以訪問執(zhí)行特定操作的特定功能。
>所有這些都是從姓名之前的雙重下劃線開始的,可以選擇包含在任何類定義中。最好理解所有魔術(shù)功能是另一個(gè)討論(這是有關(guān)魔術(shù)功能和常數(shù)的另一篇文章)。
>。> __ -construct函數(shù)一旦初始化了類,因此它是第一個(gè)將觸發(fā)的函數(shù)。在這里,我們正在添加所有掛鉤,過濾器和啟動(dòng)邏輯。 使用以下代碼更新基本輪廓。
>您可能會(huì)認(rèn)為add_action看起來與您以前看到的東西有點(diǎn)不同,并且您正確。
>
使用add_action和Add_filter在類中使用時(shí),您必須在數(shù)組中提供第二個(gè)參數(shù)。數(shù)組的第一個(gè)值是$此變量(意味著類本身),第二個(gè)是將要使用的函數(shù)的名稱(用引號(hào)包裝)。<span>//variables </span><span>private $directory = ''; </span><span>private $singular_name = 'event'; </span><span>private $plural_name = 'events'; </span><span>private $content_type_name = 'event_showcase';</span>>
有關(guān)更多信息,請(qǐng)查看WordPress codex上的add_action函數(shù)的類腳注。
> set_directory_value()
>由于我們將啟用CSS和JS,因此將文件目錄URL存儲(chǔ)到可以訪問的變量中是有意義的。我們的目錄結(jié)構(gòu)是 /包括 /event_showcase,但是可以自定義以適合您的結(jié)構(gòu)。
<span>//Theme functions for child theme </span> <span>//load our event showcase class </span><span>$event_showcase = get_stylesheet_directory() . '/includes/event_showcase/event_showcase_class.php'; </span><span>include($event_showcase);</span>check_flush_rewrite_rules()
>由于我們添加了一種自定義內(nèi)容類型,需要處理漂亮的永久鏈接;這些永久鏈接是重寫,使您的頁(yè)面URL看起來不那么丑陋(而不是讓http://mywebsite.com.au/?p=123您可以擁有http://mywebsite.com.au/my-event) >
重寫可確保您的內(nèi)容類型轉(zhuǎn)換為漂亮的格式,但是,此重寫很昂貴,并且如果過度完成,則可以減慢您的網(wǎng)站。因?yàn)橹貙懣赡軙?huì)影響您的網(wǎng)站,因此通常僅在主題/插件激活或停用的情況下更改。
>>由于您可能不想手動(dòng)更新固定鏈接,因此此功能要求WordPress自動(dòng)進(jìn)行,并且僅一次(添加了新的內(nèi)容類型之后)。
>此功能將檢查是否需要通過主題選項(xiàng)刷新網(wǎng)站。如果需要沖洗,則將更新永久鏈接,然后將選項(xiàng)的值設(shè)置為false(因此它僅觸發(fā)一次,而不是每次加載頁(yè)面)。enqueue_public_scripts_and_styles()
此功能處理前端網(wǎng)站的腳本和樣式的起點(diǎn)。
<span>/* </span><span> * Event Showcase </span><span> * Creates an 'event' content type to showcase upcoming functions and information </span><span> * Uses hooks and filters inside your theme to output relevant information </span><span> */ </span> <span>class event_showcase{ </span> <span>//variables </span> <span>private $directory = ''; </span> <span>private $singular_name = 'event'; </span> <span>private $plural_name = 'events'; </span> <span>private $content_type_name = 'event_showcase'; </span> <span>//magic function, called on creation </span> <span>public function __construct(){} </span> <span>//sets the directory (path) so that we can use this for our enqueuing </span> <span>public function set_directory_value(){} </span> <span>//check if we need to flush rewrite rules </span> <span>public function check_flush_rewrite_rules(){} </span> <span>//enqueue public scripts and styles </span> <span>public function enqueue_public_scripts_and_styles(){} </span> <span>//enqueue admin scripts and styles </span> <span>public function enqueue_admin_scripts_and_styles(){} </span> <span>//adding our new content type </span> <span>public function add_content_type(){} </span> <span>//adding meta box to save additional meta data for the content type </span> <span>public function add_meta_boxes_for_content_type(){} </span> <span>//displays the visual output of the meta box in admin (where we will save our meta data) </span> <span>public function display_function_for_content_type_meta_box($post){} </span> <span>//when saving the custom content type, save additional meta data </span> <span>public function save_custom_content_type($post_id){} </span> <span>//display additional meta information for the content type </span> <span>//@hooked using 'display_additional_meta_data' in theme </span> <span>function display_additional_meta_data(){} </span> <span>} </span> <span>//create new object </span> <span>$event_showcase = new event_showcase;</span>
我們?cè)诤艚姓{(diào)用中使用內(nèi)容類型的名稱,以便不應(yīng)該與其他插件發(fā)生任何命名沖突(因?yàn)樗鼘⑥D(zhuǎn)化為唯一的東西,即event_showcase_public_scripts`)。
我們將wp_enqueue_style和wp_enqueue_script都稱為加載我們的資源。加載資源時(shí),我們還使用$目錄和$ content_type_name屬性。
enqueue_admin_scripts_and_styles()
此功能處理管理員后端網(wǎng)站的腳本和樣式的起點(diǎn)。
>我們檢查我們是否在我們的內(nèi)容類型的編輯屏幕或新屏幕上,并且僅在我們的情況下繼續(xù)進(jìn)行(當(dāng)我們不在正確的頁(yè)面上時(shí)不需要加載資源)。??
<span>//variables </span><span>private $directory = ''; </span><span>private $singular_name = 'event'; </span><span>private $plural_name = 'events'; </span><span>private $content_type_name = 'event_showcase';</span>
我們將WP_ENQUEUE_STYLE和WP_ENQUEUE_SCRIPT稱為加載我們的資源,基本上與我們的公共面向功能相同。
在此示例中,我既使用jQuery UI DatePicker(jQuery UI庫(kù)的一部分)和JQuery UI Time Picker(構(gòu)建的樣式與日期選擇器具有類似的樣式)。>
go從上面的鏈接下載時(shí)間選擇器庫(kù),并在您的目錄中包含相關(guān)的CSS和JS文件。
add_content_type()
我們?cè)诖斯δ苤新暶魑覀兊淖远x內(nèi)容類型。
>該函數(shù)使用$ singular_name,$ plural_name和$ content_type_nameproperties來使此新帖子類型的注冊(cè)輕松。
>>這樣的構(gòu)造注冊(cè)函數(shù),這意味著您只需提供幾片信息即可輕松更改和注冊(cè)帖子類型。
>有關(guān)注冊(cè)自定義內(nèi)容類型的更詳細(xì)概述,請(qǐng)查看register_post_type函數(shù)的WordPress codex
<span>//Theme functions for child theme </span> <span>//load our event showcase class </span><span>$event_showcase = get_stylesheet_directory() . '/includes/event_showcase/event_showcase_class.php'; </span><span>include($event_showcase);</span>>添加了此步驟后,您應(yīng)該看到一個(gè)新項(xiàng)目已添加到后端WordPress菜單中。

>我們?cè)诖颂幪砑觾?nèi)容類型所需的元框。我們調(diào)用add_meta_box函數(shù)并提供OUT功能。簡(jiǎn)短的故事是,我們用它來創(chuàng)建一個(gè)新的管理員框來保存我們要保存的新表單元素。
<span>/* </span><span> * Event Showcase </span><span> * Creates an 'event' content type to showcase upcoming functions and information </span><span> * Uses hooks and filters inside your theme to output relevant information </span><span> */ </span> <span>class event_showcase{ </span> <span>//variables </span> <span>private $directory = ''; </span> <span>private $singular_name = 'event'; </span> <span>private $plural_name = 'events'; </span> <span>private $content_type_name = 'event_showcase'; </span> <span>//magic function, called on creation </span> <span>public function __construct(){} </span> <span>//sets the directory (path) so that we can use this for our enqueuing </span> <span>public function set_directory_value(){} </span> <span>//check if we need to flush rewrite rules </span> <span>public function check_flush_rewrite_rules(){} </span> <span>//enqueue public scripts and styles </span> <span>public function enqueue_public_scripts_and_styles(){} </span> <span>//enqueue admin scripts and styles </span> <span>public function enqueue_admin_scripts_and_styles(){} </span> <span>//adding our new content type </span> <span>public function add_content_type(){} </span> <span>//adding meta box to save additional meta data for the content type </span> <span>public function add_meta_boxes_for_content_type(){} </span> <span>//displays the visual output of the meta box in admin (where we will save our meta data) </span> <span>public function display_function_for_content_type_meta_box($post){} </span> <span>//when saving the custom content type, save additional meta data </span> <span>public function save_custom_content_type($post_id){} </span> <span>//display additional meta information for the content type </span> <span>//@hooked using 'display_additional_meta_data' in theme </span> <span>function display_additional_meta_data(){} </span> <span>} </span> <span>//create new object </span> <span>$event_showcase = new event_showcase;</span>display_function_for_content_type_meta_box($ post)
我們使用此功能來輸出我們內(nèi)容類型的新META信息的后端選項(xiàng)。
在我們的示例中,我們正在收集有關(guān)將向最終用戶展示的事件的日期,時(shí)間和其他信息。
我們首先將所有附加的后元元數(shù)據(jù)收集到變量中,并顯示HTML表單的輸出。其中一些字段將觸發(fā)日期選擇器接口,而其他字段會(huì)觸發(fā)時(shí)間選擇器(由元素上使用的類確定)。
>此外,我們?cè)O(shè)置了一個(gè)nonce字段,以便我們可以在此頁(yè)面上驗(yàn)證保存。>
>完成此步驟后,您應(yīng)該看到新的空元框已轉(zhuǎn)換,現(xiàn)在可以保留我們所有的新信息。
<span>//variables </span><span>private $directory = ''; </span><span>private $singular_name = 'event'; </span><span>private $plural_name = 'events'; </span><span>private $content_type_name = 'event_showcase';</span>
> save_custom_content_type($ post_id)
>我們首先檢查了我們?cè)谠蛑刑砑拥膎once并驗(yàn)證其真實(shí)性。之后,我們檢查我們是否正在執(zhí)行AutoSave,并且用戶可以實(shí)際編輯此頁(yè)面。如果所有這些檢查都通過了,我們向前邁進(jìn)。
>>我們將所有值收集到變量中,并使用Sanitize_text_field函數(shù)對(duì)它們進(jìn)行消毒。之后,我們調(diào)用update_post_meta函數(shù)以保存我們的數(shù)據(jù)。
此功能顯示我們新內(nèi)容類型的單個(gè)帖子的保存元信息(單個(gè)事件)。
> >您可以調(diào)用do_action('display_content_type_meta');從主題內(nèi)的任何地方(只要您在此內(nèi)容類型的單個(gè)帖子上),它將顯示元信息。 >我正在使用二十三十三個(gè)父母主題,所以我正在掛接我的顯示器主題的content.php。
之后
其他文件,CSS和JS
您可以看到其他字幕,日期,時(shí)間和票務(wù)數(shù)據(jù)。
從這里到哪里? >現(xiàn)在您已經(jīng)看到了一個(gè)實(shí)用的對(duì)象設(shè)計(jì)的實(shí)踐示例,以及增強(qiáng)開發(fā)的容易,您可以運(yùn)用這些技能并構(gòu)建更好的主題和插件。 希望本文將幫助您計(jì)劃開發(fā),并查看面向?qū)ο蟮姆椒ㄈ绾喂?jié)省您的時(shí)間和精力。 > WordPress中的面向?qū)ο蟮木幊蹋∣OP)是什么?這些對(duì)象用于相互交互以設(shè)計(jì)應(yīng)用程序和軟件。 WordPress中的OOP是有益的,因?yàn)樗鼮槌绦蛱峁┝艘粋€(gè)清晰的模塊化結(jié)構(gòu),這使其適合定義隱藏實(shí)現(xiàn)詳細(xì)信息并且單元具有明確定義的接口的抽象數(shù)據(jù)類型。<span>//magic function, called on creation
</span><span>public function __construct(){
</span>
<span>$this->set_directory_value(); //set the directory url on creation
</span> <span>add_action('init', array($this,'add_content_type')); //add content type
</span> <span>add_action('init', array($this,'check_flush_rewrite_rules')); //flush re-write rules for permalinks (because of content type)
</span> <span>add_action('add_meta_boxes', array($this,'add_meta_boxes_for_content_type')); //add meta boxes
</span> <span>add_action('wp_enqueue_scripts', array($this,'enqueue_public_scripts_and_styles')); //enqueue public facing elements
</span> <span>add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts_and_styles')); //enqueues admin elements
</span> <span>add_action('save_post_' . $this->content_type_name, array($this,'save_custom_content_type')); //handles saving of content type meta info
</span> <span>add_action('display_content_type_meta', array($this,'display_additional_meta_data')); //displays the saved content type meta info
</span><span>}</span>
這個(gè)操作的第二個(gè)值稱為display_additional_meta_data,與此功能相對(duì)應(yīng)。這意味著任何時(shí)候有人調(diào)用do_action('display_content_type_meta');它將調(diào)用此函數(shù)并顯示新的帖子元信息。<span>//Theme functions for child theme
</span>
<span>//load our event showcase class
</span><span>$event_showcase = get_stylesheet_directory() . '/includes/event_showcase/event_showcase_class.php';
</span><span>include($event_showcase);</span>
現(xiàn)在,大部分功能已經(jīng)完成,讓我們將其交換到我們的CSS和JS文件中以快速添加一些樣式和交互性。>
> event_showcase_admin_styles.css > event_showcase_public_styles.css
<span>/*
</span><span> * Event Showcase
</span><span> * Creates an 'event' content type to showcase upcoming functions and information
</span><span> * Uses hooks and filters inside your theme to output relevant information
</span><span> */
</span>
<span>class event_showcase{
</span>
<span>//variables
</span> <span>private $directory = '';
</span> <span>private $singular_name = 'event';
</span> <span>private $plural_name = 'events';
</span> <span>private $content_type_name = 'event_showcase';
</span>
<span>//magic function, called on creation
</span> <span>public function __construct(){}
</span> <span>//sets the directory (path) so that we can use this for our enqueuing
</span> <span>public function set_directory_value(){}
</span> <span>//check if we need to flush rewrite rules
</span> <span>public function check_flush_rewrite_rules(){}
</span> <span>//enqueue public scripts and styles
</span> <span>public function enqueue_public_scripts_and_styles(){}
</span> <span>//enqueue admin scripts and styles
</span> <span>public function enqueue_admin_scripts_and_styles(){}
</span> <span>//adding our new content type
</span> <span>public function add_content_type(){}
</span> <span>//adding meta box to save additional meta data for the content type
</span> <span>public function add_meta_boxes_for_content_type(){}
</span> <span>//displays the visual output of the meta box in admin (where we will save our meta data)
</span> <span>public function display_function_for_content_type_meta_box($post){}
</span> <span>//when saving the custom content type, save additional meta data
</span> <span>public function save_custom_content_type($post_id){}
</span> <span>//display additional meta information for the content type
</span> <span>//@hooked using 'display_additional_meta_data' in theme
</span> <span>function display_additional_meta_data(){}
</span> <span>}
</span>
<span>//create new object
</span> <span>$event_showcase = new event_showcase;</span>
> event_showcase_admin_scripts.js
<span>//variables
</span><span>private $directory = '';
</span><span>private $singular_name = 'event';
</span><span>private $plural_name = 'events';
</span><span>private $content_type_name = 'event_showcase';</span>
>在操作中完成的功能!
>
<span>//magic function, called on creation
</span><span>public function __construct(){
</span>
<span>$this->set_directory_value(); //set the directory url on creation
</span> <span>add_action('init', array($this,'add_content_type')); //add content type
</span> <span>add_action('init', array($this,'check_flush_rewrite_rules')); //flush re-write rules for permalinks (because of content type)
</span> <span>add_action('add_meta_boxes', array($this,'add_meta_boxes_for_content_type')); //add meta boxes
</span> <span>add_action('wp_enqueue_scripts', array($this,'enqueue_public_scripts_and_styles')); //enqueue public facing elements
</span> <span>add_action('admin_enqueue_scripts', array($this, 'enqueue_admin_scripts_and_styles')); //enqueues admin elements
</span> <span>add_action('save_post_' . $this->content_type_name, array($this,'save_custom_content_type')); //handles saving of content type meta info
</span> <span>add_action('display_content_type_meta', array($this,'display_additional_meta_data')); //displays the saved content type meta info
</span><span>}</span>
>所有內(nèi)容都拼湊在一起時(shí),您應(yīng)該能夠使用其他元數(shù)據(jù)創(chuàng)建一個(gè)新的“事件”。當(dāng)您查看活動(dòng)時(shí),您應(yīng)該在下面看到我的樣本>
>
>您可能想從我的GitHub頁(yè)面下載此示例的來源,然后更改它適合即將到來的項(xiàng)目或擴(kuò)展其以提供其他功能。
>面向?qū)ο蟮木幊膛cWordPress中的程序編程有何不同?
>
>如何在WordPress中的盒子中刪除OBJ?
>
>我如何學(xué)習(xí)以wordpress為導(dǎo)向的對(duì)象編程? WordPress面向?qū)ο蟮木幊?。這包括在線教程,課程和書籍。您還可以通過檢查WordPress核心代碼來學(xué)習(xí),因?yàn)樗鞘褂肙OP原理編寫的。是wordpress開發(fā)所需的面向?qū)ο蟮木幊虇幔?ancy>> > > > > >
并不是嚴(yán)格地面向?qū)ο蟮木幊?。?qiáng)烈建議使用WordPress開發(fā)。使用OOP原則可以幫助您的代碼更可讀,可重復(fù)使用且易于維護(hù)。它還可以幫助您更好地理解WordPress核心代碼。>>我可以使用WordPress插件使用面向?qū)ο蟮木幊蹋繉?shí)際上,許多WordPress插件都是使用OOP原理編寫的。這允許更復(fù)雜的功能,并可以使插件代碼更易于管理和維護(hù)。>
以上是WordPress以對(duì)象為導(dǎo)向的開發(fā)的詳細(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脫衣機(jī)

Video Face Swap
使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

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

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

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開發(fā)環(huán)境

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

SublimeText3 Mac版
神級(jí)代碼編輯軟件(SublimeText3)

使用Git管理WordPress項(xiàng)目時(shí),應(yīng)只將主題、自定義插件和配置文件納入版本控制;設(shè)置.gitignore文件以忽略上傳目錄、緩存和敏感配置;利用webhook或CI工具實(shí)現(xiàn)自動(dòng)部署并注意數(shù)據(jù)庫(kù)處理;采用兩分支策略(main/develop)進(jìn)行協(xié)作開發(fā)。這樣做可避免沖突、保障安全,并提升協(xié)作與部署效率。

使用WordPress測(cè)試環(huán)境是為了確保新功能、插件或主題在正式上線前的安全性和兼容性,避免影響真實(shí)網(wǎng)站。搭建測(cè)試環(huán)境的步驟包括:下載安裝本地服務(wù)器軟件(如LocalWP、XAMPP),創(chuàng)建站點(diǎn)、設(shè)置數(shù)據(jù)庫(kù)和管理員賬號(hào),安裝主題和插件進(jìn)行測(cè)試;復(fù)制正式網(wǎng)站到測(cè)試環(huán)境的方法是通過插件導(dǎo)出站點(diǎn)、導(dǎo)入測(cè)試環(huán)境并替換域名;使用時(shí)應(yīng)注意不使用真實(shí)用戶數(shù)據(jù)、定期清理無用數(shù)據(jù)、備份測(cè)試狀態(tài)、適時(shí)重置環(huán)境,并統(tǒng)一團(tuán)隊(duì)配置以減少差異問題。

創(chuàng)建Gutenberg塊的關(guān)鍵在于理解其基本結(jié)構(gòu)并正確連接前后端資源。1.準(zhǔn)備開發(fā)環(huán)境:安裝本地WordPress、Node.js和@wordpress/scripts;2.使用PHP注冊(cè)塊并用JavaScript定義塊的編輯和顯示邏輯;3.通過npm構(gòu)建JS文件以使更改生效;4.遇到問題時(shí)檢查路徑、圖標(biāo)是否正確或使用實(shí)時(shí)監(jiān)聽構(gòu)建避免重復(fù)手動(dòng)編譯。按照這些步驟,可以逐步實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Gutenberg塊。

在WordPress中,當(dāng)新增自定義文章類型或修改固定鏈接結(jié)構(gòu)后,需手動(dòng)刷新重寫規(guī)則,此時(shí)可通過代碼調(diào)用flush_rewrite_rules()函數(shù)實(shí)現(xiàn)。1.可在主題或插件激活鉤子中添加該函數(shù)以自動(dòng)刷新;2.僅在必要時(shí)執(zhí)行一次,如添加CPT、分類法或修改鏈接結(jié)構(gòu)后;3.避免頻繁調(diào)用以免影響性能;4.多站點(diǎn)環(huán)境下需視情況為每個(gè)站點(diǎn)單獨(dú)刷新;5.某些托管環(huán)境可能限制規(guī)則保存。此外,訪問“設(shè)置>固定鏈接”頁(yè)面點(diǎn)擊保存也可觸發(fā)刷新,適合非自動(dòng)化場(chǎng)景。

要實(shí)現(xiàn)響應(yīng)式WordPress主題設(shè)計(jì),首先要使用HTML5和移動(dòng)優(yōu)先的Meta標(biāo)簽,在header.php中添加viewport設(shè)置以確保移動(dòng)端正確顯示,并用HTML5結(jié)構(gòu)標(biāo)簽組織布局;其次,利用CSS媒體查詢實(shí)現(xiàn)不同屏幕寬度下的樣式適配,按移動(dòng)優(yōu)先原則編寫樣式,常用斷點(diǎn)包括480px、768px和1024px;第三,彈性處理圖片和布局,為圖片設(shè)置max-width:100%并使用Flexbox或Grid布局替代固定寬度;最后,通過瀏覽器開發(fā)者工具和真實(shí)設(shè)備進(jìn)行充分測(cè)試,優(yōu)化加載性能,確保響應(yīng)

tosetupredirectsinwordpressingthe.htaccessfile,locateThEfileInyourSite'sRootDirectorectoryAndDrectRectrulesabovethe#beginWordPresssection.forbasic301redirects,USETHEETHEETERECTREFTATRECTATREDERTREFTATREDERTREFTATRECTRECTATRECTRECTATREDECT301/OLD-PAGEHTTPS:

UsingsMtpForWordPresseMailSimProvesDeliverabilitialComparedComparedTothEdeDefaultPhpMail()函數(shù).1.smtpauthenticateswithyouremailserver.2.somemomehostsdisablesablephpmail()

TOINTEGRATETHIRD-PARTYAPISINTOWORDPRESS,關(guān)注臺(tái)詞:1.SelectAutableabepianDobtainCredentialslikeapikeYsoroAuthtoKensByEnterRegisteringThemSecure.2.ChooseBeteBetB??eteBetB??eteBetB??etebetInpliCityOorcustimplicityOrcustomPliCoseTompliCoseTomploomcoseusionfunctionfunctionfunctibunitiacuciencipuity forfunigation。
