在WordPress中構建自定義解決方案的基石之一是對鉤子有了解。 In and of themselves, they aren't terribly difficult to understand, and we'll be covering a short primer on them in this tutorial.
But if you're looking to get into more advanced WordPress development, it's worth knowing how to implement your own hooks as well.In this two-part series, we're going to review the WordPress hook system and how it's implemented, and we're going to take a look at how to define our own actions and過濾器。 >入門在開始之前,本教程假設您設置了一個本地開發環境,其中包括WordPress的最新副本。在撰寫本文時,這是WordPress 6.0.1。 如果您需要設置開發環境的指導,請參閱本教程。它將為您提供您需要了解的一切,以使用Web服務器設置,PHP,數據庫和WordPress的副本。 >如果您要尋找更多的內容,那麼其中包括該教程的串類提供了更多信息,例如如何安裝WordPress,Themes和themes和dublines等。因此,一旦您進行了設置,讓我們開始吧。是否可以與現有的wordpress hook一起使用,如果您可以使用JONPRESS掛鉤,則該模式也可能與您進行過任何功能。知道它具有正式名稱。
>無論如何,這就是在Wikipedia中定義的:
事件驅動的體系結構(EDA),也稱為消息驅動的體系結構,是一種軟件架構模式,是一種促進了生產,檢測,消耗和反應的方法,但現在就開始了
該軟件具有某些點,它傳播了一條發生了某些事情的消息。 >
我們作為開發人員,能夠編寫聆聽此消息的代碼,然後用自定義代碼響應它。當我們開始談論定義自己的鉤子時,我們將重新訪問此主題。目前,讓我們看一下Web開發中常見的兩個事件。 使用JavaScript 首先,想像一下您在前端開發中工作。您有一個帶有ADD_ACTION()函數的ID屬性的按鈕,可以指定運行操作掛鉤時將執行的回調函數。在我們的情況下,它告訴WordPress執行一個名為admin_menu動作掛鉤的函數。add_submenu_page()函數確定菜單選項的出現位置。第一個選項是parent slug,它設置為工具。以下是屏幕截圖: >您可以訪問codex,以了解有關WordPress中存在的Init
>
>理解此功能的簽名的關鍵很簡單:我們已經命名了Init
hook,我們需要確保在nock nook do_action()<?php<br>function tutsplus_register_post_type() {<br> <br>}<br>
之後,我們需要為該掛鉤提供功能。在這裡,我們將重構註冊郵政類型的代碼,以便它接受兩個參數,並且將在傳遞給WordPress的 regission_post_type
函數的數組中使用這兩個參數。
<?php<br>add_action( 'init', 'tutsplus_register_custom_post_type' );<br>function tutsplus_register_custom_post_type() {<br><br> // Set the action at priority of 10 and note that it accepts 2 arguments.<br> do_action( 'tutsplus_register_custom_post_type', 10, 2 );<br><br>}<br>
do_action()
>
在此處請注意,我們還將此功能從添加到特定的鉤子中刪除了此功能。取而代之的是,我們將從掛在我們自定義操作的函數的定義中來調用它。
register_post_type
<?php<br><br>function tutsplus_register_post_type( $singular, $plural ) {<br><br> $args = array(<br> 'label' => $plural,<br> 'labels' => array(<br> 'name' => $plural,<br> 'singular_name' => $singular,<br> 'add_new_item' => 'Add New Traveler',<br> 'edit_item' => 'Edit Traveler',<br> 'new_item' => 'New Traveler',<br> 'view_item' => 'View Traveler',<br> 'search_items' => 'Search Travelers',<br> 'not_found' => 'No Travelers',<br> ),<br> 'description' => 'A post type used to provide information on Time Travelers.',<br> 'public' => true,<br> 'show_ui' => true,<br> 'supports' => array(<br> 'title',<br> 'editor',<br> 'excerpt',<br> ),<br> );<br><br> register_post_type( 'time_traveler', $args );<br><br>}<br>
tutsplus_register_custom_custom_post_post_post_post_type<p><code> init> init
>中定義 tutsplus_register_custom_custom_post_post_type<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php<br>add_action( 'tutsplus_register_custom_post_type', 'tutsplus_register_time_traveler_type' );<br>function tutsplus_register_time_traveler_type() {<br> tutsplus_register_post_type( 'Time Traveler', 'Time Travelers' );<br>}<br></pre><div class="contentsignin">登入後複製</div></div>)。 <code> init <p>)。 </p>><h2>></h2>
<p>><code>tutsplus_register_custom_post_type
init
我選擇了一個定義的鉤子,因為您可以在此掛鉤,因為您可以在此掛鉤,並且可以將其作為一個掛鉤,並且可以使用該鉤子。完成的。
>註冊鉤子獨立站立很容易:您根本不將其掛鉤到預先存在的鉤子上,並且您將使用掛鉤到 admin_notices <anotices> </anotices>
>這篇文章已通過Nitish Kumar的貢獻進行了更新。 Nitish是一家網絡開發人員,在各種平台上創建電子商務網站方面具有經驗。他花時間在個人項目上工作,這些項目使他的日常生活更輕鬆或與朋友一起度過漫長的夜間散步。
以上是在WordPress中添加自定義掛鉤:自定義操作的詳細內容。更多資訊請關注PHP中文網其他相關文章!