Remove or override functions attached to a specified filter hook

There are situations when a plugin/theme use some filter hooks to change the default behaviour of the wordpress functionality and then forgets to remove the change they have applied or sometimes intentionaly because of such requirement. But this affects all over the website and causes problem, where as we want to use the default behaviour and not this changed one.

Now, the question is ” how can we override a funtion that is already applied for a particular filter hook “.
 
 
 

Option 1: remove_filter

If we know which function is called for a particular hook which is causing the issue, then we can use

[php]

[/php]

http://codex.wordpress.org/Function_Reference/remove_filter
Ex:[php][/php]

Option 2: remove_all_filters

Remove all the call_back functions those are added for a particular filter hook.(Helpfull when we don’t know the functions that are applied for a particular filter)
[php]

[/php]

http://codex.wordpress.org/Function_Reference/remove_all_filters

But again,this might cause issues as we are completly removing all the functions(whatever is the priority) for that filter hook. There is a solution too.

[php]

[/php]

Option 3: add_filter with high priority

Call a custom defined function with high priority for the filter hook.

[php]

[/php]

http://codex.wordpress.org/Function_Reference/add_filter
Default priority number is 10. So I am passing 99 as the $priority parameter.

[php]
posts}.comment_count DESC, {$wpdb->posts}.post_date DESC’;
}
return($orderBy);
}
}
?>
[/php]

Don’t forget to remove the filter once you are done with your task.

[php]

[/php]
 
 

Simple example to understand the exact situation:

* A plugin uses a function to change the default sorting order of posts.
[php]
posts}.menu_order, {$wpdb->posts}.post_date DESC’;
}
else
{
$orderBy = ‘{$wpdb->posts}.menu_order, {$wpdb->posts}.post_date DESC’;
}
return($orderBy);
}
?>
[/php]

* Now we have a widget(say Popular Posts by Comments count) which fetches and displays the most commented posts.
[php]
5,
‘orderby’ => ‘comment_count’,
‘ignore_sticky_posts’ => 1,
‘order’ => ‘DESC’
);
$test_popular_posts = new WP_Query($args);
while($test_popular_posts->have_posts()): $test_popular_posts->the_post();
?>
[/php]
[html]

[/html]
[php]

[/php]

* Now the problem is that the Popular Posts by Comments count widget does not display the posts as intended i.e. orderby most commented. ๐Ÿ™

Solution:

Write a function which will be called to override the previous funtion(mentioned above). Now call this function for that filter hook with high priority. ๐Ÿ™‚

* So the modified code will be as below:
[php]
posts}.comment_count DESC, {$wpdb->posts}.post_date DESC’;
}
return($orderBy);
}
}

add_filter(‘posts_orderby’, ‘test_posts_orderby_commentscount’, 99);
$args = array(
‘posts_per_page’ => 5,
‘orderby’ => ‘comment_count’,
‘ignore_sticky_posts’ => 1,
‘order’ => ‘DESC’
);
$test_popular_posts = new WP_Query($args);
while($test_popular_posts->have_posts()): $test_popular_posts->the_post();
?>


[/php]

Hope this post will help somebody someday !! ๐Ÿ™‚

 
 
 
 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *