在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中文网其他相关文章!