Home >Web Front-end >HTML Tutorial >WordPress Actions vs. Filters: Know the Difference
Action and filter hooks are a fundamental part of various WordPress APIs. Without them, your functionality in themes and (especially) plugins is limited.
But sometimes it’s easy to confuse the two, especially if WordPress has both action hooks and filter hooks with the same name.
In this article, I will define action and filter hooks and describe the differences between them, and I will demonstrate how to use them in themes and plugins. I'll also give some examples of when you can use them.
When you add actions and filter hooks to your code in WordPress (or hook functions to them), it helps to understand how WordPress calls actions and filters and in what order that happens. I won't go into detail about this here as we have another tutorial that does the job.
Let's start with some definitions. I'll also define action and filter hooks and functions so you can see the difference between them.
Functions are the first thing most people come across when learning WordPress development; if you've added code to your theme's functions.php file, you'll be writing a function.
Function specifies how things will happen. You write a function to query data, output content, or perform many other tasks. You can call (execute) functions directly in your theme's template files, or you can hook them to action or filter hooks. Functions can also contain template tags (such as conditional tags) to specify when the function should be applied.
I will show you different ways to execute functions later in this article.
An action hook (or action) is triggered when something happens, such as a page loading, a user logging in, or a custom action you define in your theme or plugin.
You can add your own action hooks using the do_action()
function, which I will demonstrate shortly. Any functions you hook into this action will be run at that point in the code.
Filter hooks or filters control how something happens or changes what is already output. You can use filters to output metadata in a specific format, override the plugin's text output, or prevent certain content from being displayed entirely.
You can add filters in code using the apply_filters()
function, which I will demonstrate shortly. As the word "apply" indicates, you apply the filter to existing code, whereas the action created with do_action()
is empty until you hook a function to it.
Let's look at some examples demonstrating how to use functions, operations and filters. First, we'll look at using functions directly in code without attaching them to hooks.
The following is an example of a function called directly in the template file. In my client site, I added a copyright page in the footer that contains the copyright information. The function is as follows:
if ( ! function_exists( 'compass_colophon' ) ) { function compass_colophon() { ?> <section class="colophon" role="contentinfo"> <small class="copyright left"> <?php echo compass_copyright(); ?> <a href="<?php echo home_url( '/' ) ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"> <?php bloginfo( 'name' ); ?> </a> </small><!-- #copyright --> <small class="credits right"> Powered by <a href="https://wordpress.org/">WordPress</a> and designed by <a href="https://compass-design.co.uk">Compass Design</a>. </a> </small><!-- #credits --> </section><!--#colophon--> <?php } }
This function is pluggable because I'm using it in the parent theme; if I then create a new function with the same name in the child theme, it will overwrite the function. Note that this function also contains another function compass_colophon()
, which is called directly in the code.
This function is located in my parent theme's functions.php
file. I can call it directly in my theme's footer.php
file like this:
compass_colophon();
This will output the code in the function at the location where it is called in my theme. You can also pass arguments to a function and then use those arguments inside the function.
As I will demonstrate later, this function can also be hooked to actions or filters.
I would have more flexibility if I attached it to a hook than if I called that copyright page function directly.
我可以在 footer.php 文件中的该位置添加一个操作挂钩,而不是在页脚文件中调用 compass_colophon()
函数,方法是添加以下内容:
do_action( 'compass_in_footer' );
do_action()
函数有一个强制参数,即操作的名称。您还可以选择向其添加参数。
所以现在我需要将其挂接到我的新操作挂钩,而不是调用我的版权页函数。在我的 functions.php 文件中,我将其添加到我的函数中:
add_action( 'compass_in_footer', 'compass_colophon' );
这将我的函数挂钩到 compass_in_footer
操作,这意味着我的函数内的代码将在代码中放置该操作的位置运行。第一个参数是操作钩子的名称,第二个参数是我的函数的名称。
这样做的一个优点是,您可以将多个函数挂钩到同一个操作,并且您可以设置优先级,以便它们按照您希望的顺序触发。
假设我有另一个函数想要挂钩到我的 compass_in_footer
挂钩,称为 compass_smallprint()
,其中包含更多小字:
if ( ! function_exists( compass_smallprint() ) ) { function compass_smallprint() { // contents of function here } } add_action( 'compass_in_footer', 'compass_smallprint', 20 );
你可以在这里看到我在我的 add_action()
函数中添加了第三个参数,这是优先级。默认优先级是 10
,如果将此留空,则会应用该优先级。因此,因为我没有为 compass_colophon()
函数设置优先级,所以为 compass_smallprint()
函数设置 20 将使该函数在之后运行/em> compass_colophon()
函数。
有时您想停止某个函数的运行。一种方法是创建该函数的虚拟版本,如果它是可插入的,则该函数不执行任何操作。简而言之,可插入函数允许您根据代码的执行顺序覆盖某些行为。您可能还想了解可插入函数的基础知识及其用法,以便在您还没有听说过它们时能够跟上。
实现此目的的另一种方法是使用操作挂钩。如果该函数已挂钩到操作挂钩,则可以使用 remove_action()
函数来执行此操作。因此,如果我想阻止 compass_smallprint()
函数运行,我可以将其从 compass_in_footer
操作中取消挂钩,如下所示:
remove_action( 'compass_in_footer', 'compass_smallprint', 20 );
remove_action()
函数具有三个参数:操作挂钩的名称、函数的名称以及函数最初挂钩到操作的优先级。您必须添加优先级才能使其发挥作用。
如果您想阻止所有函数执行,您还可以将所有函数从操作中取消挂钩。执行此操作时要小心,因为可能有一些您不知道的函数与您的操作挂钩。
为此,请使用 remove_all_actions()
函数:
remove_all_actions( 'compass_in_footer' );
添加优先级数字作为第二个参数只会删除挂钩到具有您指定的优先级的操作挂钩的函数,这为您提供了更多控制权。
您还可以选择将函数挂钩到过滤器挂钩。当您想要更改或覆盖某些现有代码时,可以执行此操作。当您创建过滤器挂钩(使用 apply_filters()
函数)时,您可以将其包装在主题或插件中的代码中,然后由附加到挂钩的任何过滤器进行更改。
如果您有想要覆盖默认设置的主题或插件选项,或者您正在创建可能包含被子主题覆盖的元素的父主题,这可能会很有用。
apply_filters()
函数具有三个参数:过滤器挂钩的名称、要过滤的值(即默认值)以及随后传递给的可选变量挂钩到过滤器的函数。
您可以在主题模板文件中或通过操作挂钩挂钩的函数内添加过滤器。让我们看一下这两个选项。
返回到我的 compass_colophon()
函数,我将其内容添加到 footer.php 文件中,将其转换为过滤器>apply_filters()
函数如下:
echo apply_filters( 'compass_colophon', ' <section class="colophon" role="contentinfo"> <small class="copyright left"> <?php echo compass_copyright(); ?> <a href="<?php echo home_url( '/' ) ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"> <?php bloginfo( 'name' ); ?> </a> </small><!-- #copyright --> <small class="credits right"> Powered by <a href="http://wordpress.org/">WordPress</a> and designed by <a href="http://compass-design.co.uk">Compass Design</a>. </a> </small><!-- #credits --> </section><!--#colophon-->' );
这将输出我设置为 apply_filters()
函数的第二个参数的代码。
但是,我不想将其直接添加到我的主题模板文件中,因此我会将过滤器添加到我已经通过操作挂钩附加的函数中。
因此,我使用 do_action()
函数将 compass_in_footer
操作添加到我的 footer.php 文件中,如上所示,然后我在我的 functions.php 文件中创建一个函数,该函数与该操作挂钩并包含一个过滤器:
if ( ! function_exists( 'compass_colophon' ) ) { function compass_colophon() { echo apply_filters( 'compass_colophon_filter', ' <section class="colophon" role="contentinfo"> <small class="copyright left"> <?php echo compass_copyright(); ?> <a href="<?php echo home_url( '/' ) ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"> <?php bloginfo( 'name' ); ?> </a> </small><!-- #copyright --> <small class="credits right"> Powered by <a href="http://wordpress.org/">WordPress</a> and designed by <a href="http://compass-design.co.uk">Compass Design</a>. </a> </small><!-- #credits --> </section><!--#colophon-->' ); } add_action( 'compass_in_footer', 'compass_colophon' );
这意味着我现在可以通过以下三种方式之一覆盖默认内容:
compass_colophon()
的新函数,该函数会覆盖我的父主题中的函数,因为它是可插入的compass_in_footer
操作挂钩中取消 compass_colophon()
函数,并编写一个新函数并将其附加到其位置compass_colophon_filter
过滤器挂钩,该挂钩会覆盖 apply_filters()
函数中的值在现实生活中,您不需要有这么多选项,因此您更有可能将过滤器应用于函数中的部分内容,而不是整个内容。
因此我可以创建两个过滤器,一个用于版权部分,另一个用于制作人员:
if ( ! function_exists( 'compass_colophon' ) ) { function compass_colophon() { echo '<section class="colophon" role="contentinfo">'; echo apply_filters( 'compass_copyright_filter', ' <small class="copyright left"> <?php echo compass_copyright(); ?> <a href="<?php echo home_url( '/' ) ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"> <?php bloginfo( 'name' ); ?> </a> </small><!-- #copyright -->' ); echo apply_filters( 'compass_credit_filter', ' <small class="credits right"> Powered by <a href="http://wordpress.org/">WordPress</a> and designed by <a href="http://compass-design.co.uk">Compass Design</a>. </a> </small><!-- #credits -->' ); echo '</section><!--#colophon-->'; } add_action( 'compass_in_footer', 'compass_colophon' );
然后我可以通过取消挂钩或在子主题中编写一个新函数来覆盖整个 compass_colophon 函数,或者我可以创建一个挂钩到 compass_copyright_filter
或 compass_credits_filter
过滤器挂钩,单独覆盖每个元素。
要将函数挂钩到过滤器挂钩,请使用 add_filter()
函数,该函数有两个参数:挂钩名称和函数名称。
因此,要更改学分,我会编写以下函数:
function new_credits() { ?> <small class="credits right"> Powered by <a href="http://wordpress.org/">WordPress</a> and designed by <a href="http://rachelmccollin.co.uk">Rachel McCollin</a>. </a> </small><!-- #credits --> <?php } add_filter( 'compass_credits_filter', 'new_credits' );
这会用我的 new_credits()
函数的内容覆盖我原来的 compass_credits_filter
过滤器挂钩中设置的值,但将其他所有内容保留在 compass_colophon()
功能相同。
您还可以在将函数挂钩到过滤器时指定优先级,其方式与操作挂钩完全相同。优先级较低的函数将首先运行。
与操作挂钩一样,您也可以从过滤器挂钩中删除函数。您可以使用 remove_filter()
函数来执行此操作,该函数具有三个参数:过滤器挂钩的名称、函数的名称和优先级,如果在函数最初挂钩时设置了优先级,则优先级是强制性的过滤器。
因此,要删除我的 new_credits()
函数,我使用这个:
remove_filter( 'compass_credits_filter', 'new_credits' );
代码输出将恢复为我在原始 apply_filters()
函数中指定的值。因此,如果我想删除 new_credits()
函数并且没有任何内容出现在其位置,我必须添加一个新函数。然后,我取消第一个函数的挂钩并挂钩我的新函数,如下所示:
function no_credits() { return; } remove_filter( 'compass_credits_filter', 'new_credits' ); add_filter( 'compass_credits_filter', 'no_credits' );
让我们鸟瞰整个事情,以更好地理解它是如何组合在一起的。本教程的主要目标是将内容输出到页脚,并以一种使其他人可以轻松修改页脚内容的方式进行。
最简单的方法是直接调用 footer.php 文件中的函数。然而,这剥夺了函数执行及其输出的一些灵活性。克服这个限制的一种方法是使用钩子,这就是我们接下来所做的。
我们将对 compass_colophon()
函数的调用替换为对 do_action()
的调用。请记住,do_action()
函数没有调用 compass_colophon()
。它只是在 do_action()
调用的位置创建了一个操作挂钩。在我们的例子中,操作挂钩的名称是 compass_in_footer
。
触发 compass_in_footer
操作时我们要调用的实际函数是通过调用 add_action()
函数来指定的。它将我们的钩子名称作为它的第一个参数,我们的回调函数作为第二个参数。我们附加到 compass_in_footer
操作的第一个回调函数是 compass_colophon()
函数。
我们还可以选择将多个回调函数附加到同一个钩子。这正是我们添加 compass_smallprint()
函数作为操作挂钩的另一个回调时所做的事情。调用函数的顺序由传递给 add_action()
的第三个参数的值决定。这使我们能够确保 compass_smallprint()
函数在 compass_colophon()
之后执行。
One advantage of using action hooks is that when an action is triggered, you can also stop the execution of any attached callback functions by passing their name to the remove_action()
function.
At this point, we have an action hook that fires in the footer, and we call different callback functions attached to that action hook. Basically, we take some action and output the content we want to place in the footer of our website. Others can also unhook our function and attach their own callback functions to the action hook.
What if you only want to partially change the output of the footer, without completely overriding or canceling the original function? This is when filters prove useful.
We use the apply_filters()
function to create a new filter hook. It accepts at least two parameters. The first is the name of the filter hook, which we set to compass_copyright_filter
and compass_credit_filter
in the two apply_filters()
calls. The second parameter is the value we want to filter or modify. We set it up to display the HTML code for copyright and credit information.
We can then hook our own functions to these filters. We do this with the help of add_filter()
function. This function accepts two parameters. The first parameter is the name of the filter hook, and the second parameter is the name of the callback function to use to filter the values.
Another important thing to remember is that the value passed to apply_filters()
will only be filtered if a callback is attached using the add_filter()
function. Otherwise, the value passed to apply_filters()
as the second argument will remain unchanged.
We use this feature to change the end credits but keep the copyright information in the footer intact.
Now that you know how to add actions in WordPress, maybe you want to expand your coding skills. Once WordPress has applied the filter, you can follow our tutorial to learn other tricks:
Understanding the difference between action hooks and filter hooks and being able to use them effectively will facilitate your theme and plugin development. In fact, you simply can't write a plugin without using at least one type of hook, since the only way to activate the code in a plugin is through its attached action and filter hooks.
This guide shows you how to add the same functionality using functions, action hooks, and one or more filter hooks, as well as techniques for removing functions from hooks and advice on when each technique is more useful.
In addition to hooking functions to the action and filter hooks you create, you can also hook them to WordPress-provided actions and filters, such as the wp_head
action or the body_class
filter.
The above is the detailed content of WordPress Actions vs. Filters: Know the Difference. For more information, please follow other related articles on the PHP Chinese website!