WordPress is an amazing platform and the most popular content management system in the world. The reason for this title is because of its scalability. In this series, we’ll learn about filters – one of the best ways to scale WordPress.
The basis of this feature is Hooks - Filters and Actions for WordPress. Without them, we wouldn’t be able to take advantage of the true scalability of WordPress.
In this series, we’ll learn about filters, which are one of the best ways to scale WordPress. This will be a seven-part series focusing on:
There are hundreds of WordPress filters in the core, these 50 examples are only a subset of them (about 10%), so there may be an appendix if you like the series and suggest new examples for new filters.
Anyway, it’s time to introduce WordPress filters. let's start!
In Codex, the filter is defined as follows:
Filters are functions that WordPress passes data through at certain points during execution, before taking certain actions on the data. Filters sit between the database and the browser, and between the browser and the database; most input and output in WordPress will go through at least one filter. WordPress does some filtering by default, and your plugin can add your own.
So, essentially, a filter is a function that processes website data before WordPress outputs it. Filters are one of two types of hooks in WordPress – the other is called Actions, which is the subject of another series of articles.
Although it seems like a complex topic, filters (and operations) are really easy to understand. I was also intimidated when I first encountered them, but after seeing how simple they were, I was able to learn about hundreds of filters and operations just by checking the Codex or digging into the core code.There are hundreds of filters you should definitely know about. But first, you need to know how to use them.
Using filters in WordPress
In this section, we will discuss four things:
Suppose we are building a plugin to remove vowels in post titles. Instead of saying "remove the vowels in my post title", you say "hook this function (remove vowels) to the filter of my post title".
Is it complicated? Not really. In the following example, we will code a function that removes vowels from
anything:
<?php function remove_the_vowels( $title ) { $title = preg_replace( '/[aeiou]/i', '', $title ); return $title; } ?>
$title string, removes the vowels and returns it. Easy, right? Now, let's take it to the next level and connect it to a filter:
<?php function remove_the_vowels( $title ) { $title = preg_replace( '/[aeiou]/i', '', $title ); return $title; } add_filter( 'the_title', 'remove_the_vowels' ); ?>
add_filter():
<?php add_filter( $tag, $function_to_add, $priority, $accepted_args ); ?>
: if the setting is lower, the function will run earlier; if the setting is higher, the function will run later.
.
remove_filter(). Let's see how it works:
<?php remove_filter( $tag, $function_to_remove, $priority ); ?>
add_filter():
还有另一个名为 remove_all_filters()
的函数,它只有两个参数($tag
和 $priority
),您可以在其中设置过滤器的名称并设置优先级。顾名思义,它会删除所有与过滤器挂钩的函数。
想知道这些过滤器是如何创建的?有一个名为 apply_filters()
的特殊函数,它围绕核心代码创建数百个过滤器。当然,它可以在核心之外使用,这意味着我们也可以在插件和主题内创建过滤器。
让我们看看它是如何工作的:
<?php apply_filters( $tag, $value, $var1, $var2 /* ...and so on */ ); ?>
add_filter()
挂钩的过滤器函数修改的值。让我们考虑一个例子:假设您编写了一个仅返回 Peter Griffin 的名言的函数:
<?php function peter_griffin_quote() { $quote = "The bird is the word."; return $quote; } ?>
apply_filters()
函数,如下所示:
<?php function peter_griffin_quote() { $quote = "The bird is the word."; return apply_filters( 'peter_griffin_quote', $quote ); } ?>
<?php function change_the_quote( $quote ) { $quote = str_replace( 'bird', 'nerd', $quote ); return $quote; } add_filter( 'peter_griffin_quote', 'change_the_quote' ); ?>
现在,每次运行 peter_griffin_quote()
函数时,Peter 的引用都会略有更改,而无需开发人员编辑您的插件文件。小菜一碟!
如果您需要有关此主题的更多信息,您应该查看 Pippin Williamson 关于 Tuts+ Code 的精彩教程:“使用操作和过滤器编写可扩展插件”。在本教程中,您可以学习如何为您的插件或主题创建过滤器和操作。
您处理得越多,使用滤镜的乐趣就越多。它们有数百种,学习每一种都可以让您更接近成为一名 WordPress 大师。在本系列的下一部分中,我们将了解 10 个 WordPress 过滤器:
login_errors
comment_post_redirect
allowed_redirect_hosts
body_class
locale
sanitize_user
the_content
the_password_form
the_terms
wp_mail_from
我对这个系列感到非常兴奋,希望您能像我一样喜欢它。如果您认为您可以通过建议更多过滤器并要求更多示例来帮助我完成教程,请随时告诉我并通过下面的评论分享您的想法。
如果您喜欢本文中所读的内容,请不要忘记分享!
下一个教程见!
The above is the detailed content of Introduction to WordPress Filters: A Comprehensive Overview of 50 Filters. For more information, please follow other related articles on the PHP Chinese website!