What are WordPress filters?
A WordPress filter is a hook that modifies data before it is returned to the database and displayed to the user. As a result, you can customize the functionality of a plugin, theme or website without changing the core WordPress files. For example, adding footnotes to all site posts or changing their excerpt length. There are four common filter hook functions - add_filter, remove_filter, has_filter and doing_filter.
![图片[1]-WordPress 中的过滤器是什么?如何使用 WordPress add_filter ?-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/04/image-224.png)
How WordPress Filters Work
Filters intercept data passed by WordPress. They modify it according to the given functionality and display it to the user's web browser. To help you understand, check out the following code snippet:
// Specify the filter and the callback function
add_filter( 'example_filter', 'example_callback' ); // Define the callback function.
// Define the callback function
function example_callback( $example ) {
// alter $example by concatenating on it
$example . ' add a text at the end '; }
return $example; }
}
The following is an explanation of the example:
- utilizationadd_filterfunction sets a filter that will modify the data. In our example, the filter name isexample_filterThe
- Define the callback function that will be run when WordPress finds the filter, i.e.example_callbackThe
- Specifies how the callback function will modify the$exampleThe data indicated by the parameter. In the snippet, we will use a period to connect it to the sentence at the end.
- utilizationreturnmethod displays the modified data to the user's Web browser.
To apply the filter hook to your WordPress site, write the code to the currently active theme'sfunctions.phpDocumentation.
![图片[2]-WordPress 中的过滤器是什么?如何使用 WordPress add_filter ?-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/04/image-186.png)
It is recommended that the code be added to thesubtheme(used form a nominal expression)filein the theme root folder to prevent the code from being lost after a theme update. Or create a separate file in the theme root folder to hold the filter hooks and call it in the functions.php file. This will better organize your code and ensure it is not lost when the theme is updated.
include _once( get_template_directory(), '/yourfilename.php':'
If you don't have access to the theme root folder, install the plugin to add custom code directly from the WordPress admin dashboard. Plugins for this task are availableCode SnippetsThe
Attention:modificationsfunctions.phpfile may result in an error or the site becoming inaccessible. To avoid this, before continuingCreating a BackupThe
WordPress add_filter parameters
WordPress add_filterThe function has four parameters. Each of them is shown below:
- hook name. The callback function you want to hook to the Filter name.
- callback function. The callback function to run when the filter is applied.
- Priorities. An optional parameter that specifies the order of execution of the functions associated with the filter. The default value is10(math.) genus The smaller the number, the earlier the implementation.
- Accepted arguments. The number of parameters to pass to the hook function. This parameter is optional and the default value is1The
The function and argument syntax is as follows:
add_filter( hook_name, callback_function, priority=1,
accepted_arguments=1);
Example of add_filter in WordPress changing the length of a WordPress post excerpt:
function custom_excerpt_length( $length ) {
return 20; // change the number here to adjust the length of the excerpt
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
![图片[3]-WordPress 中的过滤器是什么?如何使用 WordPress add_filter ?-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/04/image-225.png)
In this example, we create a custom function named custom_excerpt_length that accepts a parameter, $length, indicating the length of the excerpt. We then pass this function as an argument to the add_filter function, specifying the name of the filter (excerpt_length) and the name of our custom function. The final 999 parameter indicates the priority, and here we set it to a higher value to ensure that our filter is applied last.
Show images for specific post categories
WordPress filter hooks allow you to customize posts with specific IDs or categories. In the code snippet below, we have added an icon for each post belonging to a premium category:
add_filter( 'the_content', 'add_disclaimer' );
function add_disclaimer( $content )
{
// concatenate the content to modify it
return $content .
"<br><center><strong>All writings and opinions are my own".
}
In the example, we created thethe_contentfilter and connect it to thewpb_content_filterCallback function hook. This function specifies the conditions under which the filter is applied.
In this case, if The post belongs tohigh levelcategory, the filter function will change the content by adding images to it.
![图片[4]-WordPress 中的过滤器是什么?如何使用 WordPress add_filter ?-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/04/image-187.png)
Change the number of products displayed
Filters also allow you to modify the eCommerce plugin to customize your online store. For example, the following customization code changes the number of products displayed on the WooCommerce plugin:
add_filter('storefront_products_per_page','alter_sf_products_per_page' );
function alter_sf_products_per_page()
{
// change the default value
function alter_sf_products_per_page() { // change the default value.
}
In the code snippet, we create thestorefront_products_per_pageCustomize the filter with thealter_sf_products_per_pagefunction hook. When data passes through it, the filter will call the function to remove the default value from the12change to3and display it to the user's browser.
![图片[5]-WordPress 中的过滤器是什么?如何使用 WordPress add_filter ?-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/04/image-188.png)
Other Popular WordPress Filters
In addition to add_filterIn addition to this, there are several other functions that developers can use with filter hooks.
Delete Filter ()
This function removes functions that are hooked to a specific filter. Web developers often use it to remove default WordPress functions without having to remove the code entirely. The syntax is as follows:
remove_filter( hook_name, callback_function, priority = 10 );
Execute Filter()
This function checks if another function is currently being executed. It takes the hook name as an argument and outputstruemaybefalseReturn value. The code is as follows:
if doing_filter( 'hook_name' )
{
// execute a script if the filter is running
}
![图片[6]-WordPress 中的过滤器是什么?如何使用 WordPress add_filter ?-光子波动网 | 专业WordPress修复服务,全球范围,快速响应](https://www.361sale.com/wp-content/uploads/2024/04/image-226.png)
Yes_Filter()
The has_filter function verifies that the filter was successfully applied to the hook. It takes the filter name as its first argument and the callback function as an optional second argument.
To check for a specific function, the second parameter must be included. Otherwise, it will returntrueto indicate that any function is hooked into the filter. The code for this function is as follows:
has_filter( 'hook_name', 'callback_function' );
Summary:
A filter is a WordPress hook used to modify data before it is displayed on a website. Web developers use it to customize plugins or themes in order to add extra functionality to their websites.
The filter hook will intercept the data passed by WordPress, change it according to your needs, and display it to the visitor's browser. To set up a filter, add the filter hook to the theme'sWrite add_filter in the functions.php file.function with the hook name and callback function as arguments.
Example use cases for filter hooks include changing the length of article excerpts and adding disclaimers to website posts. In addition toadd_filterOther than that, its other functions includeremove_filter,doing_filterrespond in singinghas_filterThe
Link to this article:https://www.361sale.com/en/7288The article is copyrighted and must be reproduced with attribution.
- newest
- hottest
See only the author.