>以對象為導(dǎo)向的代碼無處不在,WordPress也不例外。
WordPress的核心構(gòu)建在用於控制所有內(nèi)容的一系列對象/類上 在本教程中,我們將研究面向?qū)ο蟮脑O(shè)計,以及如何在為WordPress開發(fā)時如何在實際應(yīng)用中使用這些設(shè)計,包括:
準(zhǔn)確地了解哪種面向?qū)ο蟮脑O(shè)計是。
>- 討論為什麼我們應(yīng)該使用面向?qū)ο蟮脑O(shè)計。
- >檢查一個真實的示例以展示如何工作。
- >注意1:另請注意,該教程主要概述了以WordPress為中心的概念,因此,如果您是面向?qū)ο蟮脑O(shè)計的全部新概念,則可能應(yīng)該通過PHP參考指南快速概述OO。
- >注2:如果您渴望立即下載所有內(nèi)容,請隨時從我的github存儲庫下載源並與教程一起關(guān)注。
>
鑰匙要點WordPress組中的面向?qū)ο蟮脑O(shè)計(OOD)相關(guān)的功能和信息中的信息,增強代碼組織和維護。
> OOD提供了諸如通過封裝,通過繼承的可伸縮性和模塊化的更好的組織提供好處,以便於代碼重複使用和管理。
>教程提供了在WordPress中創(chuàng)建自定義事件展示的一個實踐示例,證明了OOD原理的實現(xiàn)。 示例中的>關(guān)鍵步驟包括設(shè)置目錄結(jié)構(gòu),創(chuàng)建類文件,並實現(xiàn)用於添加和顯示自定義內(nèi)容類型和元框的功能。
該指南強調(diào)了OOD比程序編程的優(yōu)勢,強調(diào)組織,可伸縮性和模塊化。- >教程以行動呼籲從GitHub下載源代碼,鼓勵讀者在其WordPress開發(fā)項目中應(yīng)用所示的OOD概念。
- 無論如何,什麼是面向?qū)ο蟮脑O(shè)計? >
- >對象方向設(shè)計(OO設(shè)計)是解決問題的不同方法,與傳統(tǒng)的程序方法分開。 >使用對象設(shè)計的設(shè)計,您可以創(chuàng)建一個結(jié)構(gòu)(類),該結(jié)構(gòu)(類)將將所有相關(guān)功能(方法)和信息(屬性)組合在一起以解決問題。
- > 這與您創(chuàng)建函數(shù)和變量以解決問題的過程開發(fā)不同,但是,這些元素可以分離出多個文件,重新播出並經(jīng)常與之相關(guān)。 簡而言之,對象的設(shè)計是將相關(guān)功能匯總到一個位置,以便可以從單個位置維護,擴展和使用。
- 創(chuàng)建一種自定義內(nèi)容類型來管理這些“事件”。
- 添加其他元框來處理其他信息處理。 >
- 保存其他元信息。
- 顯示我們的“事件”的元信息。
- > event_showcase_public_styles.css
- > event_showcase_public_scripts.js
- 您應(yīng)該有類似於以下類似的東西:
- >以後您需要下載並將jQuery Time Picker腳本和樣式放置在此目錄中。不用擔(dān)心這個。
- __ construct() - 我們所有的鉤子,過濾器和初始化代碼都將進入此處。 >
- set_directory_value() - 定義我們的目錄屬性,該屬性將使我們輕鬆地插入我們在CSS或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)在瀏覽一下,更新這些元素並概述代碼 >
為什麼對象方向是個好主意?
>由於程序代碼運行良好,因此您可能想知道以對象為導(dǎo)向的方法開發(fā)的意義是什麼?
>雖然程序設(shè)計沒有“錯誤”,但以更面向?qū)ο蟮臉邮介_發(fā)有幾個好處。
>組織>使用對象導(dǎo)向方法時,通常會更好地組織代碼。創(chuàng)建類意味著解決問題的功能都在文件中(與程序開發(fā)不同,您可能擁有函數(shù)和變量到處都有)。
將所有代碼都放在一個地方可以幫助您保持井井有條,並協(xié)助其他可能會出現(xiàn)並需要了解您的系統(tǒng)的開發(fā)人員。
>
>沒有什麼比撿起一個項目並不得不四處尋找的函數(shù)和變量更糟糕的是,這些功能和變量用於創(chuàng)建網(wǎng)站的各種元素(由於未知原因而始終分佈在多個文件上) 。>
可伸縮性>以對象為導(dǎo)向的方法求解問題時,您可以創(chuàng)建類。
>
可以通過使用繼承擴展這些類來擴展或縮放這些類。繼承將使您基於另一個類創(chuàng)建一個類,該類可訪問其所有屬性和方法。>您可以擴展一類以執(zhí)行其他功能,甚至可以覆蓋其方法和屬性以迎合這種情況。
>使用程序代碼,您要么需要重構(gòu)代碼的一部分,要么完全創(chuàng)建新代碼以處理其他情況。模塊化
對象設(shè)計的目的是封裝所有功能,以將情況求解到類中。
>由於解決問題的所有代碼都在類內(nèi)部,並且該類通常在其自己的PHP文件中,因此您可以將整個文件放在另一個位置,因此它本質(zhì)上是模塊化的。>這樣的好處是,如果您編寫足夠好的課程,則應(yīng)該可以在以後重複使用它們,並且只需執(zhí)行輕微的自定義。
>>您可以復(fù)制和粘貼程序代碼,但通常情況不太井井有條,您可能必須瀏覽幾個文件以確保您擁有所需的功能。
在這種情況下,我們的客戶希望在其網(wǎng)站上可以創(chuàng)建和管理事件的位置。這些活動將用於促進即將到來的瑣事之夜,賓果遊戲和其他有趣的活動。
>第一步是分解所需的功能,以便我們可以計劃解決方案(在這裡進行良好的計劃將有助於我們以對象為導(dǎo)向的方法進行)。在我們的情況下,我們可以看到以下一組任務(wù):我們可以創(chuàng)建一個插件,也可以編輯子主題以提供此功能。
因為我們希望所有內(nèi)容都可以容易地管理和模塊化,所以讓我們從“孩子主題”內(nèi)部設(shè)置文件夾結(jié)構(gòu)開始。
>組織所有內(nèi)容的簡便方法是創(chuàng)建孩子主題中的包含目錄。該目錄將包含您的主題包含/實施的其他代碼或功能(您可以隨意調(diào)用此功能,目的是在主題中擁有一個單一目錄)。
在此目錄中,我們應(yīng)該製作另一個文件夾,這次稱為event_showcase。在此文件夾中,我們將在此情況下放置所有適用的文件。 我們還需要一個JS和一個CSS文件夾。
>將功能組織到文件夾中將意味著您可以更輕鬆地查看哪些模塊 /功能您的主題工具。例如,您可能需要為主題實現(xiàn)另一組功能,您可以添加一個新文件夾,從邏輯上講,所有內(nèi)容都整齊地組織了。
>讓我們?yōu)槲覀兊氖纠齽?chuàng)建文件,然後將它們移至正確的目錄>
> event_showcase_class.php
> event_showcase_admin_styles.css
- > event_showcase_admin_scripts.js
>將文件加載到WordPress
中
>打開您的孩子主題的函數(shù)。 php,並將以下代碼放在頂部或底部
>>這將加載主類文件,該文件將概述我們的功能。在此類課程中,我們將處理腳本和樣式的重新設(shè)計以及其他元素。
創(chuàng)建類概述
>我們的類文件稱為event_showcase_class.php將包含用於我們的事件系統(tǒng)的大多數(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>>將以下代碼複製到您的類文件中。
<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ù)制此文件並更改一些屬性,並且類將處理其他更改)。
>我們在類的頂部聲明了一些將通過我們的方法引用的屬性。這些目的是使它們更容易重新置換此類並在將來更改其功能。
__ 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ù)是一個魔術(shù)函數(shù)。這些功能是類的特殊PHP5功能,可以訪問執(zhí)行特定操作的特定功能。
>所有這些都是從姓名之前的雙重下劃線開始的,可以選擇包含在任何類定義中。最好理解所有魔術(shù)功能是另一個討論(這是有關(guān)魔術(shù)功能和常數(shù)的另一篇文章)。
>。> __ -construct函數(shù)一旦初始化了類,因此它是第一個將觸發(fā)的函數(shù)。在這裡,我們正在添加所有掛鉤,過濾器和啟動邏輯。 使用以下代碼更新基本輪廓。
>您可能會認(rèn)為add_action看起來與您以前看到的東西有點不同,並且您正確。
>
使用add_action和Add_filter在類中使用時,您必須在數(shù)組中提供第二個參數(shù)。數(shù)組的第一個值是$此變量(意味著類本身),第二個是將要使用的函數(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>>
有關(guān)更多信息,請查看WordPress codex上的add_action函數(shù)的類腳註。
> set_directory_value()
>由於我們將啟用CSS和JS,因此將文件目錄URL存儲到可以訪問的變量中是有意義的。我們的目錄結(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)容類型,需要處理漂亮的永久鏈接;這些永久鏈接是重寫,使您的頁面URL看起來不那麼醜陋(而不是讓http://mywebsite.com .au/?p=123您可以擁有http://mywebsite.com.au/my-event) >
重寫可確保您的內(nèi)容類型轉(zhuǎn)換為漂亮的格式,但是,此重寫很昂貴,並且如果過度完成,則可以減慢您的網(wǎng)站。因為重寫可能會影響您的網(wǎng)站,因此通常僅在主題/插件激活或停用的情況下更改。
>>由於您可能不想手動更新固定鏈接,因此此功能要求WordPress自動進行,並且僅一次(添加了新的內(nèi)容類型之後)。
>此功能將檢查是否需要通過主題選項刷新網(wǎng)站。如果需要沖洗,則將更新永久鏈接,然後將選項的值設(shè)置為false(因此它僅觸發(fā)一次,而不是每次加載頁面)。enqueue_public_scripts_and_styles()
此功能處理前端網(wǎng)站的腳本和樣式的起點。
<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>
我們在呼叫調(diào)用中使用內(nèi)容類型的名稱,以便不應(yīng)該與其他插件發(fā)生任何命名衝突(因為它將轉(zhuǎn)化為唯一的東西,即event_showcase_public_scripts`)。
我們將wp_enqueue_style和wp_enqueue_script都稱為加載我們的資源。加載資源時,我們還使用$目錄和$ content_type_name屬性。
enqueue_admin_scripts_and_styles()
此功能處理管理員後端網(wǎng)站的腳本和樣式的起點。
>我們檢查我們是否在我們的內(nèi)容類型的編輯屏幕或新屏幕上,並且僅在我們的情況下繼續(xù)進行(當(dāng)我們不在正確的頁面上時不需要加載資源)。 ??
<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庫的一部分)和JQuery UI Time Picker(構(gòu)建的樣式與日期選擇器具有類似的樣式)。>
go從上面的鏈接下載時間選擇器庫,並在您的目錄中包含相關(guān)的CSS和JS文件。
add_content_type()
我們在此功能中聲明我們的自定義內(nèi)容類型。
>該函數(shù)使用$ singular_name,$ plural_name和$ content_type_nameproperties來使此新帖子類型的註冊輕鬆。
>>這樣的構(gòu)造註冊函數(shù),這意味著您只需提供幾片信息即可輕鬆更改和註冊帖子類型。
>有關(guān)註冊自定義內(nèi)容類型的更詳細概述,請查看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)該看到一個新項目已添加到後端WordPress菜單中。

>我們在此處添加內(nèi)容類型所需的元框。我們調(diào)用add_meta_box函數(shù)並提供OUT功能。簡短的故事是,我們用它來創(chuàng)建一個新的管理員框來保存我們要保存的新表單元素。
<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信息的後端選項。
在我們的示例中,我們正在收集有關(guān)將向最終用戶展示的事件的日期,時間和其他信息。
我們首先將所有附加的後元元數(shù)據(jù)收集到變量中,並顯示HTML表單的輸出。其中一些字段將觸發(fā)日期選擇器接口,而其他字段會觸發(fā)時間選擇器(由元素上使用的類確定)。
>此外,我們設(shè)置了一個nonce字段,以便我們可以在此頁面上驗證保存。>
>完成此步驟後,您應(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)
>我們首先檢查了我們在元框中添加的nonce並驗證其真實性。之後,我們檢查我們是否正在執(zhí)行AutoSave,並且用戶可以實際編輯此頁面。如果所有這些檢查都通過了,我們向前邁進。
>>我們將所有值收集到變量中,並使用Sanitize_text_field函數(shù)對它們進行消毒。之後,我們調(diào)用update_post_meta函數(shù)以保存我們的數(shù)據(jù)。
此功能顯示我們新內(nèi)容類型的單個帖子的保存元信息(單個事件)。
> >您可以調(diào)用do_action('display_content_type_meta');從主題內(nèi)的任何地方(只要您在此內(nèi)容類型的單個帖子上),它將顯示元信息。 >我正在使用二十三十三個父母主題,所以我正在掛接我的顯示器主題的content.php。
之後
其他文件,CSS和JS
您可以看到其他字幕,日期,時間和票務(wù)數(shù)據(jù)。
從這裡到哪裡? >現(xiàn)在您已經(jīng)看到了一個實用的對象設(shè)計的實踐示例,以及增強開發(fā)的容易,您可以運用這些技能並構(gòu)建更好的主題和插件。 希望本文將幫助您計劃開發(fā),並查看面向?qū)ο蟮姆椒ㄈ绾喂?jié)省您的時間和精力。 > WordPress中的面向?qū)ο蟮木幊蹋∣OP)是什麼?這些對像用於相互交互以設(shè)計應(yīng)用程序和軟件。 WordPress中的OOP是有益的,因為它為程序提供了一個清晰的模塊化結(jié)構(gòu),這使其適合定義隱藏實現(xiàn)詳細信息並且單元具有明確定義的接口的抽像數(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>
這個操作的第二個值稱為display_additional_meta_data,與此功能相對應(yīng)。這意味著任何時候有人調(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)容都拼湊在一起時,您應(yīng)該能夠使用其他元數(shù)據(jù)創(chuàng)建一個新的“事件”。當(dāng)您查看活動時,您應(yīng)該在下面看到我的樣本>
>
>您可能想從我的GitHub頁面下載此示例的來源,然後更改它適合即將到來的項目或擴展其以提供其他功能。
>面向?qū)ο蟮木幊膛cWordPress中的程序編程有何不同?
>
>如何在WordPress中的盒子中刪除OBJ?
>
>我如何學(xué)習(xí)以wordpress為導(dǎo)向的對象編程? WordPress面向?qū)ο蟮木幊?。這包括在線教程,課程和書籍。您還可以通過檢查WordPress核心代碼來學(xué)習(xí),因為它是使用OOP原理編寫的。 是wordpress開發(fā)所需的面向?qū)ο蟮木幊虇幔?> > > > > >
並不是嚴(yán)格地面向?qū)ο蟮木幊獭娏医ㄗh使用WordPress開發(fā)。使用OOP原則可以幫助您的代碼更可讀,可重複使用且易於維護。它還可以幫助您更好地理解WordPress核心代碼。>>我可以使用WordPress插件使用面向?qū)ο蟮木幊??實際上,許多WordPress插件都是使用OOP原理編寫的。這允許更複雜的功能,並可以使插件代碼更易於管理和維護。 >
以上是WordPress以對象為導(dǎo)向的開發(fā)的詳細內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

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

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

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

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

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

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

熱門話題

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

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

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

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

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

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

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

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