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 isHooks- 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:
Filtersare 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 thatprocesses website data before WordPress outputs it.Filtersare one of two types of hooks in WordPress – the other is calledActions, 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 As I said, using WordPress filters is very simple. Just to understand some of the basic features, you need to understand what each filter does. (To be honest, the hardest part is learning all the filters, but as you can imagine, you can't learn every filter all at once - you need to learn them as and when you need them.) In this section, we will discuss four things:anything:
$titlestring, removes the vowels and returns it. Easy, right? Now, let's take it to the next level and connect it to a filter:
add_filter():
: 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:
add_filter():
还有另一个名为remove_all_filters()
的函数,它只有两个参数($tag
和$priority
),您可以在其中设置过滤器的名称并设置优先级。顾名思义,它会删除所有与过滤器挂钩的函数。
想知道这些过滤器是如何创建的?有一个名为apply_filters()
的特殊函数,它围绕核心代码创建数百个过滤器。当然,它可以在核心之外使用,这意味着我们也可以在插件和主题内创建过滤器。
让我们看看它是如何工作的:
add_filter()
挂钩的过滤器函数修改的值。让我们考虑一个例子:假设您编写了一个仅返回 Peter Griffin 的名言的函数:
apply_filters()
函数,如下所示:
现在,每次运行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!