{"id":85876,"date":"2023-05-17T15:55:15","date_gmt":"2023-05-17T15:55:15","guid":{"rendered":"\/tutorials\/?p=85876"},"modified":"2025-04-23T09:17:33","modified_gmt":"2025-04-23T09:17:33","slug":"wordpress-add-filter","status":"publish","type":"post","link":"\/tutorials\/wordpress-add-filter\/","title":{"rendered":"What Are Filters in WordPress and How to Use WordPress add_filter\u00a0"},"content":{"rendered":"<p>A WordPress filter is a hook that manipulates internal data before it is displayed on the browser. To create a filter, add the <strong>add_filter <\/strong>function in your website&rsquo;s <strong>functions.php <\/strong>file.<\/p><p>In WordPress development, users use this filter to modify a plugin, a theme, or specific website functionality. For example, you can change your website posts&rsquo; excerpt length or add a footer note to the articles.<\/p><p>In this WordPress tutorial, we will explain how to use the <strong>add_filter <\/strong>function to create a filter hook. We will also explore the function&rsquo;s parameters and provide its common use case examples.<\/p><p class=\"has-text-align-center\"><a href=\"https:\/\/assets.hostinger.com\/content\/tutorials\/pdf\/Mega-WordPress-Cheat-EN.pdf\" target=\"_blank\" rel=\"noopener\">Download all-in-one WordPress cheat sheet<\/a><\/p><p>\n\n\n\n\n\n<div class=\"protip\"><div class=\"protip__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path d=\"M1.49234 23.5024C1.23229 23.5024 0.972242 23.4024 0.782206 23.2123C0.562165 22.9923 0.452144 22.6822 0.502153 22.3722C0.562165 21.9221 1.14227 17.9113 3.00262 16.351C3.63274 15.8209 4.43289 15.5509 5.26305 15.5609C6.09321 15.5909 6.87335 15.9109 7.47347 16.4911C8.6937 17.6913 8.76371 19.6717 7.6435 20.9919C6.0832 22.8523 2.08245 23.4324 1.63237 23.4924C1.59236 23.4924 1.54235 23.4924 1.50234 23.4924L1.49234 23.5024ZM5.16303 17.5613C4.84297 17.5613 4.53291 17.6713 4.29287 17.8813C3.60274 18.4614 3.07264 19.9317 2.75258 21.242C4.06282 20.9219 5.5331 20.3918 6.11321 19.7017C6.55329 19.1716 6.54329 18.3814 6.0832 17.9213C5.85316 17.7013 5.5431 17.5713 5.20304 17.5613C5.19304 17.5613 5.17303 17.5613 5.16303 17.5613ZM11.7243 21.8821C11.4942 21.8821 11.2642 21.8021 11.0841 21.652C10.8541 21.462 10.7241 21.1819 10.7241 20.8819V15.9109L8.08358 13.2705H3.11264C2.81259 13.2705 2.53254 13.1404 2.3425 12.9104C2.15246 12.6803 2.07245 12.3803 2.12246 12.0902C2.19247 11.7102 2.84259 8.36953 4.70294 7.12929C6.33325 6.04909 8.96375 6.49918 10.244 6.80923C11.5442 4.96889 13.2546 3.4286 15.2349 2.33839C17.4553 1.11816 19.9858 0.518051 22.4963 0.498047C23.0464 0.498047 23.4865 0.948132 23.4865 1.49824C23.4865 5.0389 22.3763 9.97983 17.1753 13.7605C17.4853 15.0408 17.9354 17.6613 16.8552 19.2816C15.615 21.1419 12.2744 21.7921 11.8943 21.8621C11.8343 21.8721 11.7743 21.8821 11.7143 21.8821H11.7243ZM12.7245 16.181V19.6016C13.7146 19.2916 14.7948 18.7915 15.2049 18.1814C15.675 17.4812 15.605 16.091 15.385 14.9008C14.5248 15.3808 13.6346 15.8109 12.7245 16.181ZM9.66388 12.0302L11.9643 14.3307C13.1845 13.8306 14.3648 13.2204 15.485 12.5103C19.9358 9.51974 21.2361 5.60901 21.4561 2.53843C19.6157 2.67846 17.8254 3.20856 16.2051 4.09872C14.2847 5.14892 12.6544 6.68921 11.4942 8.54956C10.7841 9.65977 10.174 10.82 9.66388 12.0302ZM4.39289 11.2701H7.81353C8.1936 10.3599 8.63368 9.46974 9.11377 8.60957C7.92355 8.38953 6.51329 8.31952 5.81315 8.78961C5.19304 9.19968 4.70294 10.3099 4.39289 11.2701Z\" fill=\"#673DE6\"\/>\n                <\/svg>\n                <p class=\"protip__title\">\n                   What Is a WordPress Filter?\n                <\/p>\n            <\/div>\n            <p class=\"protip__content\"><br>\nA WordPress filter is a hook that modifies data before it is returned to the database and displayed to the users. As a result, you can customize plugins, themes, or websites&rsquo; functionality without altering the WordPress core files.<br>\n<br>\nFor example, add a footnote to all website posts or change their excerpt length. There are four common filter hook functions &ndash; <strong>add_filter<\/strong>,  <strong>remove_filter<\/strong>, <strong>has_filter<\/strong>, and <strong>doing_filter<\/strong>.<\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-how-wordpress-filters-work\">How WordPress Filters Work<\/h3><p>In short, filters intercept data that WordPress passes. They modify it based on the given function and display it to users&rsquo; web browsers. To help you understand, look at this snippet:<\/p><pre class=\"wp-block-preformatted\">\/\/ Specify the filter and the callback function\n   add_filter( 'example_filter', 'example_callback' );\n   \/\/ Define the callback function\n   function example_callback( $example ) {\n      \/\/ alter $example by concatenating on it\n      $example . &lsquo; add a text at the end &rsquo;;\n      return $example;\n   }<\/pre><p>Here&rsquo;s the explanation of the example:<\/p><ol class=\"wp-block-list\">\n<li>Set a filter using the <strong>add_filter <\/strong>function that will modify the data. In our case, the filter name is <strong>example_filter<\/strong>.<\/li>\n\n\n\n<li>Define the callback function that will run when WordPress finds the filter, namely <strong>example<\/strong>_<strong>callback<\/strong>.<\/li>\n\n\n\n<li>Specify how the callback function will modify the data, which the <strong>$example<\/strong> argument represents. In the snippet, we will use the full stop to concatenate it with a sentence at the end.<\/li>\n\n\n\n<li>Display the modified data to the user&rsquo;s web browser using the <strong>return <\/strong>method.<\/li>\n<\/ol><p>To apply a filter hook to your WordPress website, write the code in your currently-active theme&rsquo;s <strong>functions.php <\/strong>file. If you use Hostinger&rsquo;s <a href=\"\/wordpress-hosting\">WordPress hosting service<\/a>, open <strong>File Manager <\/strong>and navigate to <strong>public_html\/wp-content\/themes\/yourthemefolder<\/strong>.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/05\/The-functions.php-file-location-in-File-Manager.png\"><img decoding=\"async\" width=\"1024\" height=\"554\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/The-functions.php-file-location-in-File-Manager.png\/w=1024,h=1024,fit=scale-down\" alt=\"The functions.php file location in hPanel's File Manager\" class=\"wp-image-85882\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/The-functions.php-file-location-in-File-Manager.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/The-functions.php-file-location-in-File-Manager.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/The-functions.php-file-location-in-File-Manager.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/The-functions.php-file-location-in-File-Manager.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div><p>Furthermore, we recommend adding the code to your <a href=\"\/tutorials\/how-to-create-wordpress-child-theme\/\">child theme&rsquo;s<\/a> file to prevent it from disappearing after an update. Alternatively, create a separate file for the filter hooks in the theme root folder and write the following code in the <a href=\"\/tutorials\/functions-php-wordpress\/\">functions.php<strong> <\/strong>file<\/a> to call it:<\/p><pre class=\"wp-block-preformatted\">include _once( get_template_directory(), &lsquo;\/yourfilename.php&rsquo;:&rsquo;<\/pre><p>If you can&rsquo;t access the theme root folder, <a href=\"\/tutorials\/wordpress\/how-to-install-wordpress-plugins\/\">install a plugin<\/a> to add the custom code directly from your WordPress admin dashboard. A popular plugin for this task is <a href=\"https:\/\/wordpress.org\/plugins\/code-snippets\/\" target=\"_blank\" rel=\"noopener\">Code Snippets<\/a>.<\/p><p>\n\n\n<div class=\"announcement-block announcement-block--important\">\n            <span class=\"announcement-block__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path fill-rule=\"evenodd\" clip-rule=\"evenodd\"\n                          d=\"M12 22.5C17.799 22.5 22.5 17.799 22.5 12C22.5 6.20101 17.799 1.5 12 1.5C6.20101 1.5 1.5 6.20101 1.5 12C1.5 17.799 6.20101 22.5 12 22.5ZM13.637 7.65198C13.637 6.74791 12.9041 6.01501 12 6.01501C11.0959 6.01501 10.363 6.74791 10.363 7.65198C10.5335 9.53749 10.875 13.383 10.875 13.383C10.875 14.0043 11.3787 14.508 12 14.508C12.6213 14.508 13.125 14.0043 13.125 13.383V13.38L13.637 7.65198ZM11.9927 15.714C11.3714 15.714 10.8677 16.2177 10.8677 16.839C10.8677 17.4603 11.3714 17.964 11.9927 17.964H12.0073C12.6286 17.964 13.1323 17.4603 13.1323 16.839C13.1323 16.2177 12.6286 15.714 12.0073 15.714H11.9927Z\"\n                          fill=\"#FEA419\"\/>\n                <\/svg>\n                Important\n            <\/span>\n            <p class=\"announcement-block__content\">\n                <strong>Important!<\/strong> Modifying the <strong>functions.php<\/strong> file may lead to an error or an inaccessible website. To avoid this, <a href=\"https:\/\/www.hostinger.com\/tutorials\/backups\/downloading-website-backup\/\">create a backup<\/a> before proceeding.\n            <\/p><\/div>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-wordpress-add-filter-parameters\">WordPress add_filter Parameters<\/h2><p>The WordPress <strong>add_filter <\/strong>function has four parameters. Here&rsquo;s each of them:<\/p><ul class=\"wp-block-list\">\n<li><strong>Hook name<\/strong>.<strong> <\/strong>The filter name<strong> <\/strong>you want to hook the callback functions into.<\/li>\n\n\n\n<li><strong>Callback function<\/strong>. The callback function to be run when the filter is applied.<\/li>\n\n\n\n<li><strong>Priority<\/strong>. An optional parameter specifying the execution order of functions associated with a filter. The default value is <strong>10<\/strong>,<strong> <\/strong>and the lower the number, the earlier it executes.<\/li>\n\n\n\n<li><strong>Accepted arguments<\/strong>. The number of arguments passed to the hooked functions. This parameter is optional and has the default value of <strong>1<\/strong>.<\/li>\n<\/ul><p>The function and parameters syntax is as follows:<\/p><pre class=\"wp-block-preformatted\">add_filter( hook_name, callback_function, priority=1, \n   accepted_arguments=1);<\/pre><h2 class=\"wp-block-heading\" id=\"h-examples-of-add-filter-in-wordpress\">Examples of add_filter in WordPress<\/h2><p>The WordPress filter hooks let users change or add various new functions to their websites. For your reference, this section will explore some example code snippets.<\/p><h3 class=\"wp-block-heading\" id=\"h-changing-post-excerpt-length\">Changing Post Excerpt Length<\/h3><p>A simple example use case of a WordPress filter is changing the default posts&rsquo; excerpt length. Here&rsquo;s the code snippet:<\/p><pre class=\"wp-block-preformatted\"> add_filter( &lsquo;excerpt_length&rsquo;, &lsquo;change_length&rsquo; );\n   function change_length( $length )\n   {\n      \/\/ change the default excerpt length\n      return 20;\n   }<\/pre><p>In the snippet, we created a function that changes the post excerpt length, which the <strong>$length <\/strong>argument represents. Then, we hooked the function to the <strong>excerpt_length <\/strong>filter.<\/p><p>When the data passes through the filter, the function will run and alter it accordingly. Then, the <strong>return <\/strong>method will limit the excerpt length to <strong>20 <\/strong>and pass it to the user.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/05\/Modified-WordPress-posts-snippet-length.png\"><img decoding=\"async\" width=\"1458\" height=\"916\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/Modified-WordPress-posts-snippet-length.png\/w=1024,h=1024,fit=scale-down\" alt=\" Modified WordPress posts snippet length\" class=\"wp-image-85883\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/Modified-WordPress-posts-snippet-length.png\/w=1458,fit=scale-down 1458w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/Modified-WordPress-posts-snippet-length.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/Modified-WordPress-posts-snippet-length.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/Modified-WordPress-posts-snippet-length.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/Modified-WordPress-posts-snippet-length.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1458px) 100vw, 1458px\" \/><\/a><\/figure>\n<\/div><p>\n\n<div class=\"announcement-block announcement-block--important\">\n            <span class=\"announcement-block__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path fill-rule=\"evenodd\" clip-rule=\"evenodd\"\n                          d=\"M12 22.5C17.799 22.5 22.5 17.799 22.5 12C22.5 6.20101 17.799 1.5 12 1.5C6.20101 1.5 1.5 6.20101 1.5 12C1.5 17.799 6.20101 22.5 12 22.5ZM13.637 7.65198C13.637 6.74791 12.9041 6.01501 12 6.01501C11.0959 6.01501 10.363 6.74791 10.363 7.65198C10.5335 9.53749 10.875 13.383 10.875 13.383C10.875 14.0043 11.3787 14.508 12 14.508C12.6213 14.508 13.125 14.0043 13.125 13.383V13.38L13.637 7.65198ZM11.9927 15.714C11.3714 15.714 10.8677 16.2177 10.8677 16.839C10.8677 17.4603 11.3714 17.964 11.9927 17.964H12.0073C12.6286 17.964 13.1323 17.4603 13.1323 16.839C13.1323 16.2177 12.6286 15.714 12.0073 15.714H11.9927Z\"\n                          fill=\"#FEA419\"\/>\n                <\/svg>\n                Important\n            <\/span>\n            <p class=\"announcement-block__content\">\n                <strong>Important!<\/strong> For some WordPress themes, you must also edit the <strong>content.php<\/strong> file to apply the excerpt filter. Read our tutorial on <a href=\"\/tutorials\/wordpress-excerpt-length\/\">how to change the WordPress excerpt length<\/a> to learn more about it.\n            <\/p><\/div>\n\n\n\n<\/p><h3 class=\"wp-block-heading\" id=\"h-modifying-posts-content\">Modifying Posts Content<\/h3><p>A common use of the <strong>add_filter <\/strong>function is adding new content to your website posts. For example, here&rsquo;s a code snippet example that places a disclaimer at the bottom of every website post:<\/p><pre class=\"wp-block-preformatted\"> add_filter( &lsquo;the_content&rsquo;, &lsquo;add_disclaimer&rsquo; );\n   function add_disclaimer( $content )\n   {\n      \/\/ concatenate the content to modify it\n      return $content . \n      &ldquo;&lt;br&gt;&lt;center&gt;&lt;strong&gt;All writings and opinions are my own&ldquo;;\n   }<\/pre><p>The example has the <strong>the_content<\/strong> filter, which hooks to the <strong>add_disclaimer <\/strong>callback function. When the filter runs, this function will modify <strong>$content <\/strong>by concatenating it with the <strong>All writings and opinions are my own <\/strong>disclaimer message.<\/p><p>The code will return the modified content to the browsers with the disclaimer at the bottom.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/05\/A-disclaimer-message-underneath-a-WordPress-post.png\"><img decoding=\"async\" width=\"1460\" height=\"500\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/A-disclaimer-message-underneath-a-WordPress-post.png\/w=1024,h=1024,fit=scale-down\" alt=\"A disclaimer message underneath a WordPress post\" class=\"wp-image-85884\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/A-disclaimer-message-underneath-a-WordPress-post.png\/w=1460,fit=scale-down 1460w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/A-disclaimer-message-underneath-a-WordPress-post.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/A-disclaimer-message-underneath-a-WordPress-post.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/A-disclaimer-message-underneath-a-WordPress-post.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/A-disclaimer-message-underneath-a-WordPress-post.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1460px) 100vw, 1460px\" \/><\/a><\/figure>\n<\/div><h3 class=\"wp-block-heading\" id=\"h-showing-image-to-a-specific-post-category\">Showing Image to a Specific Post Category<\/h3><p>The WordPress filter hooks let you customize posts with a specific ID or category. In the following snippet, we added an icon to every post that belongs to the premium category:<\/p><pre class=\"wp-block-preformatted\">add_filter( 'the_content', 'content_filter' );\n   function content_filter( $content ) \n   {\n      \/\/ condition where the function applies\n      if ( in_category('premium') )\n      $content = sprintf(\n         '&lt;img class=\"premium-icon\"    \n         src=\"%s\/directory\/filename.png\"alt=\"Premium Content Icon\"  \n         title=\"\" \/&gt;%s', get_bloginfo( 'stylesheet_directory' ), \n         $content);\n      return $content;\n   }<\/pre><p>In the example, we created the <strong>the_content <\/strong>filter and hooked it with the <strong>wpb_content_filter <\/strong>callback function. The function specifies the condition where the filter will apply.<\/p><p>In this case, if<strong> <\/strong>the post belongs to the <strong>premium <\/strong>category, the filter function will alter the content by adding an image to it.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/05\/An-icon-on-a-post-from-a-specific-category.png\"><img decoding=\"async\" width=\"1460\" height=\"970\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/An-icon-on-a-post-from-a-specific-category.png\/w=1024,h=1024,fit=scale-down\" alt=\"An icon on a post from a specific category\" class=\"wp-image-85885\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/An-icon-on-a-post-from-a-specific-category.png\/w=1460,fit=scale-down 1460w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/An-icon-on-a-post-from-a-specific-category.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/An-icon-on-a-post-from-a-specific-category.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/An-icon-on-a-post-from-a-specific-category.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/An-icon-on-a-post-from-a-specific-category.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1460px) 100vw, 1460px\" \/><\/a><\/figure>\n<\/div><h3 class=\"wp-block-heading\" id=\"h-changing-the-number-of-displayed-products\">Changing the Number of Displayed Products<\/h3><p>A filter also lets you modify an eCommerce plugin to customize your online store. For example, here&rsquo;s a custom code that changes the number of displayed products on the WooCommerce plugin:<\/p><pre class=\"wp-block-preformatted\">add_filter('storefront_products_per_page','alter_sf_products_per_page' );\n   function alter_sf_products_per_page() \n   {\n      \/\/ change the default value\n      return 3 ;\n   }<\/pre><p>In the snippet, we created the <strong>storefront_products_per_page <\/strong>custom filter and hooked it with the <strong>alter_sf_products_per_page <\/strong>function. When the data passes through it, the filter will call the function to change the default value from <strong>12<\/strong> to <strong>three<\/strong> and display it to the users&rsquo; browsers.<\/p><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/05\/Title-and-alt-Products-on-an-eCommerce-WordPress-website_s-storefront.png\"><img decoding=\"async\" width=\"1024\" height=\"584\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/Title-and-alt-Products-on-an-eCommerce-WordPress-website_s-storefront.png\/w=1024,h=1024,fit=scale-down\" alt=\"Products on an eCommerce WordPress website's storefront\" class=\"wp-image-85886\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/Title-and-alt-Products-on-an-eCommerce-WordPress-website_s-storefront.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/Title-and-alt-Products-on-an-eCommerce-WordPress-website_s-storefront.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/Title-and-alt-Products-on-an-eCommerce-WordPress-website_s-storefront.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2023\/05\/Title-and-alt-Products-on-an-eCommerce-WordPress-website_s-storefront.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div><p>\n\n        <div class=\"protip\">\n            <div class=\"protip__heading\">\n                <svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n                    <path d=\"M1.49234 23.5024C1.23229 23.5024 0.972242 23.4024 0.782206 23.2123C0.562165 22.9923 0.452144 22.6822 0.502153 22.3722C0.562165 21.9221 1.14227 17.9113 3.00262 16.351C3.63274 15.8209 4.43289 15.5509 5.26305 15.5609C6.09321 15.5909 6.87335 15.9109 7.47347 16.4911C8.6937 17.6913 8.76371 19.6717 7.6435 20.9919C6.0832 22.8523 2.08245 23.4324 1.63237 23.4924C1.59236 23.4924 1.54235 23.4924 1.50234 23.4924L1.49234 23.5024ZM5.16303 17.5613C4.84297 17.5613 4.53291 17.6713 4.29287 17.8813C3.60274 18.4614 3.07264 19.9317 2.75258 21.242C4.06282 20.9219 5.5331 20.3918 6.11321 19.7017C6.55329 19.1716 6.54329 18.3814 6.0832 17.9213C5.85316 17.7013 5.5431 17.5713 5.20304 17.5613C5.19304 17.5613 5.17303 17.5613 5.16303 17.5613ZM11.7243 21.8821C11.4942 21.8821 11.2642 21.8021 11.0841 21.652C10.8541 21.462 10.7241 21.1819 10.7241 20.8819V15.9109L8.08358 13.2705H3.11264C2.81259 13.2705 2.53254 13.1404 2.3425 12.9104C2.15246 12.6803 2.07245 12.3803 2.12246 12.0902C2.19247 11.7102 2.84259 8.36953 4.70294 7.12929C6.33325 6.04909 8.96375 6.49918 10.244 6.80923C11.5442 4.96889 13.2546 3.4286 15.2349 2.33839C17.4553 1.11816 19.9858 0.518051 22.4963 0.498047C23.0464 0.498047 23.4865 0.948132 23.4865 1.49824C23.4865 5.0389 22.3763 9.97983 17.1753 13.7605C17.4853 15.0408 17.9354 17.6613 16.8552 19.2816C15.615 21.1419 12.2744 21.7921 11.8943 21.8621C11.8343 21.8721 11.7743 21.8821 11.7143 21.8821H11.7243ZM12.7245 16.181V19.6016C13.7146 19.2916 14.7948 18.7915 15.2049 18.1814C15.675 17.4812 15.605 16.091 15.385 14.9008C14.5248 15.3808 13.6346 15.8109 12.7245 16.181ZM9.66388 12.0302L11.9643 14.3307C13.1845 13.8306 14.3648 13.2204 15.485 12.5103C19.9358 9.51974 21.2361 5.60901 21.4561 2.53843C19.6157 2.67846 17.8254 3.20856 16.2051 4.09872C14.2847 5.14892 12.6544 6.68921 11.4942 8.54956C10.7841 9.65977 10.174 10.82 9.66388 12.0302ZM4.39289 11.2701H7.81353C8.1936 10.3599 8.63368 9.46974 9.11377 8.60957C7.92355 8.38953 6.51329 8.31952 5.81315 8.78961C5.19304 9.19968 4.70294 10.3099 4.39289 11.2701Z\" fill=\"#673DE6\"\/>\n                <\/svg>\n                <p class=\"protip__title\">\n                    Suggested Reading                <\/p>\n            <\/div>\n            <p class=\"protip__content\"> Learn more about WordPress Function:<br>\n<a href=\"\/tutorials\/wordpress-get-post-meta\/\">How to Use WordPress get_post_meta Function<\/a><\/p>\n                    <\/div>\n        \n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-other-commonly-used-wordpress-filters\">Other Commonly Used WordPress Filters<\/h2><p>In addition to <strong>add_filter<\/strong>, here are several other functions that developers may use with the filter hook.<\/p><p><strong>remove_filter()<\/strong><\/p><p>This function removes a function hooked to a specific filter. Web developers commonly use this to delete the default WordPress function without removing the code entirely. Here&rsquo;s the syntax:<\/p><pre class=\"wp-block-preformatted\">remove_filter( hook_name, callback_function, priority = 10 );<\/pre><p><strong>doing_filter()<\/strong><\/p><p>This function checks if another function is currently being executed. It takes a hook name as the parameter and outputs either a <strong>true <\/strong>or <strong>false <\/strong>return value. The syntax is as follows:<\/p><pre class=\"wp-block-preformatted\">if doing_filter( 'hook_name&rsquo; )\n{ \n\/\/ execute a script if the filter is running \n};<\/pre><p><strong>has_filter()<\/strong><\/p><p>The <strong>has_filter <\/strong>function verifies if a filter is successfully applied to a hook. It takes a filter name as the first parameter and the callback function as the optional second one.<\/p><p>To check a specific function, you must include the second parameter. Otherwise, it will return <strong>true <\/strong>to indicate that any function is hooked to the filter. The function&rsquo;s syntax is as follows:<\/p><pre class=\"wp-block-preformatted\">has_filter( &lsquo;hook_name&rsquo;, &lsquo;callback_function&rsquo; );<\/pre><figure class=\"wp-block-image size-large\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-wordpress-hosting\" href=\"\/wordpress-hosting\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"2048\" height=\"600\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=1024,h=1024,fit=scale-down\" alt=\"\" class=\"wp-image-111781\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2024\/06\/New-WP_in-text-banner.png\/w=1536,fit=scale-down 1536w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2><p>A filter is a WordPress hook for modifying data before displaying it on the website. Web developers use it to customize a plugin or a theme to add extra functionality to their websites.<\/p><p>The filter hook will intercept data WordPress passes, alter it based on your needs, and display it to visitors&rsquo; browsers. To set a filter, write the <strong>add_filter <\/strong>function with the hook name and the callback function as the parameters in your theme&rsquo;s <strong>functions.php <\/strong>file.<\/p><p>The filter hook&rsquo;s example use cases include changing article excerpt length and adding a disclaimer to website posts. In addition to <strong>add_filter<\/strong>, its other functions include <strong>remove_filter<\/strong>, <strong>doing_filter<\/strong>, and <strong>has_filter<\/strong>.<\/p><p>Should you have any questions or comments, feel free to leave a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A WordPress filter is a hook that manipulates internal data before it is displayed on the browser. To create a [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/tutorials\/wordpress-add-filter\/\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":337,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"How to Use WordPress add_filter + Useful Examples","rank_math_description":"WordPress add_filter attaches a function to a hook. Check out this article to learn what WordPress Filters are and how to use the add_filter().","rank_math_focus_keyword":"wordpress add_filter","footnotes":""},"categories":[22643,22637],"tags":[],"class_list":["post-85876","post","type-post","status-publish","format-standard","hentry","category-advanced","category-wordpress"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/wordpress-add-filter","default":0},{"locale":"pt-BR","link":"https:\/\/www.hostinger.com\/br\/tutoriais\/add-filter-wordpress","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/add-filter-wordpress","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/add-filter-wordpress","default":0},{"locale":"id-ID","link":"https:\/\/www.hostinger.com\/id\/tutorial\/cara-menggunakan-wordpress-add_filter","default":0},{"locale":"en-GB","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/wordpress-add-filter","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/what-are-filters-in-wordpress-and-how-to-use-wordpress-add_filter","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/what-are-filters-in-wordpress-and-how-to-use-wordpress-add_filter","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/add-filter-wordpress","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/add-filter-wordpress","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/add-filter-wordpress","default":0},{"locale":"pt-PT","link":"https:\/\/www.hostinger.com\/pt\/tutoriais\/add-filter-wordpress","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/wordpress-add-filter","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/wordpress-add-filter","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/wordpress-add-filter","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/wordpress-add-filter","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/85876","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/users\/337"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/comments?post=85876"}],"version-history":[{"count":17,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/85876\/revisions"}],"predecessor-version":[{"id":96312,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/posts\/85876\/revisions\/96312"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/media?parent=85876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/categories?post=85876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/tutorials\/wp-json\/wp\/v2\/tags?post=85876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}