Widely Used WordPress Filter Functions

I have covered up some of the aspects of using WordPress filter hooks in the previous article Quick Tutorial on WordPress Filters For Plugin Development. In this article, I will discuss some of the Widely Used WordPress Filter Functions for plugin development.

 

current_filter

This function gives you the name of the currently executed filter hook. But keep in mind that current_filter() doesn’t work with filter hooks, it just applies to action hooks to get the name of the current filter or action hook. WordPress developers use this function when they are using a single function for many hook but they want the function to trigger differently depending on the hook currently firing.

For example, let’s say you want to remove some certain words from post content and titles. And you want to use some words in the post title, but the actual set of words is identical for the post content. In this scenario, you can utilize current_filter() function as a single function to filter the_title and the_content.

Following code snippet will teach you how you can set an array of unwanted words according to situation and replace them with any word like “Hi” in the text.

[code type=”php”]
<?php

add_filter(‘the_title’, ‘replacement_of_unwanted_words’);
add_filter(‘the_content’, ‘replacement_of_unwanted_words’);

function replacement_of_unwanted_words($test) {

//Check if the_title is filter hook then set its unwanted words to anything
if(‘the_title’ == current_filter())
$specific_words = array (‘something’, ‘evil’);

//Similarly with the_content
if(‘the_content’ == current_filter())
$specific_words = array(‘something’, ‘Okay’, ‘Ji’);

//Now we have arrays of unwanted words in arrays, so replace the unwanted words

$text = str_replace($specific_words, ‘Hi’, $text);

//Return the values

return $text;

}
?>
[/code]

has_filter

The has_filter() function is a conditional filter function to specify if any other filter has been registers for a specific hook. It has two parameters. First one is the name of the filter hook to check and second one is a function to check against the filter. This function by default returns false in case there is no filter found for the given hook, and returns true if found. Checkout the following code snippet for better understanding:

[code type=”php”]
if(has_filter(‘the_title’))
{
echo ‘found’;
}
else { echo ‘Not Found’; }
[/code]

remove_all_filters

In some cases when you are developing a WordPress plugin, you may need to remove filters which has some specific priority from a filter hook or all the filters from the specific filter hook. In this case you can use remove_all_filters() function. The function has 2 parameters: first one is the name of the filter hook which you want to remove and second one is the priority of the filters to remove from the filter hook. Following code snippet will teach you how to remove default formatting, for example, auto paragraphs to their character entity equivalents for post content.

[code type=”php”]
<?php
remove_all_filters(‘the_content’);
?>
[/code]

remove_filter

This function is used to remove filters that already have been registered for a specific filter hook. Note that this function is only used after you have registered a filter using add_filter() function. Now there is one interesting thing in wp-includes/default-filters.php. You will find a function wpautop() which is capable to convert double line breaks into HTML paragraphs.

Now if you don’t want HTML paragraph when there is double breaks in the code editor. In this case you can remove wpautop() hook with remove_filter(). Here is the example:

[code type=”php”]
<?php
remove_filter(‘the_content’, ‘wpautop’);
?>
[/code]

apply_filters_ref_array

The function works like apply_filters() but there is a huge difference between the both and that is parameters which are passed. This function accepts an array of arguments rather than accepting multiple values. You should note the point somewhere in a safe place  😉 that the passed parameter should be by reference not value. It has two parameters: first one is name of the filter hook and second one is an array of arguments to pass.

Now, for example, you have a scenario that you want to execute a complex database query for loading posts for the home page of the WordPress website that you aren’t able to achieve via other normal WordPress functions. Checkout the following code snippet:

[code type=”php”]
< ?php

add_filter( ‘getting_posts_results’, ‘home_page_posts’ );

function home_page_posts( $results ) {

global $wpdb, $wp_query;

// Check if current page is home page
if ( is_home() ) {

// Posts per page.
$get_per_page = get_option( ‘posts_per_page’ );

// Get the current page.
$paged = get_query_var( ‘paged’ );

// Set the $page variable.
$page = ( ( 0 == $paged || 1 == $paged ) ? 1 : absint( $paged ) );

// Set the number of posts to offset. *

$offset = ( $page – 1 ) * $per_page . ‘, ‘;

// Setting up the limit by the number of posts to show and $offset.
$limits = ‘LIMIT ‘. $offset . $per_page;

// Grab the results from the database.
$results = $wpdb- > get_results( ”
SELECT SQL_CALC_FOUND_ROWS $wpdb- > posts.*
FROM $wpdb- > posts
WHERE 1=1
AND post_type = ‘page’
AND post_status = ‘publish’
ORDER BY post_title ASC
$limits
” );
}
return $results;
}
? >
[/code]

Duan Lingxin

Duan Lingxin

Content crafter and chief editor at Scratching Info. Also regular contributor on other major online tech platforms. Security Specialist by day and a writer by night, he does his best to instill his knowledge about tech while delivering inspiring and life changing resources through his writing,

More Posts - Website

Leave a Comment