In the first post of the series, we introduced the idea of the world of filters within the context of WordPress. In this tutorial, we're going to start reviewing 50 selected filters by explaining what they do and see an example for each filter.

So, without further ado, here's the first batch of our 50 filters!

Changing the Default Login Error Messages

WordPress gives out too much information when it displays a login error: "The password you entered for the username barisunver is incorrect." That means hackers could try different usernames to find out which username we use. You can disable these kinds of messages thanks to this filter.

Example: Fixing the "TMI Issue"

Let's just change all the error messages to an empty string to prevent hackers finding out about our credentials:

Redirecting Commenters to Another Page

After posting a comment on WordPress, you stay on the same page. Yes, it's the logical thing to do, but did you know you can make your commenters redirect to another page after a successful comment? The comment_post_redirect filter lets us do that and I think I found the perfect example for this filter!

Example: Redirect User to a Subscription Page After a Successful Comment

Commenters are more likely to "follow" your blog posts, right? So if we redirect them to a page saying "Thank you for your comment! Would you like to subscribe to my blog?", we could turn those commenters to subscribers! And it's an easy task to do, too, with the help of our lovely comment_post_redirect filter:

Adding classes to <body>

The body_class() function is simply awesome. It provides different classes for the body tag on different pages so you can use your CSS file(s) more effectively. And with the filter with the same name, body_class, you can add or remove classes.

Example: Adding Category Classes to the <body> Tags of Post Pages

If you need different styling for each of your categories, the body_class filter can help you with that by adding classes with category names to the body tags of your single post pages.

If you have a category named "World", you can use the .cat-world class to style posts in the "World" category.

Changing the Locale

WordPress is the #1 content management system in the world because it allows us to translate almost every bit of text in it. And the locale filter lets us set the locale for our WordPress installations.

Example: Changing the Language of the Website With a URL Parameter

If you have a multilingual website, an easy solution to a quick language switch would be via a URL parameter, like below:

Now, when you set a url parameter like mywebsite.com/?language=tr, the locale of your website will be changed into Turkish. Not the post content, of course, but it's a start.

Filtering Sanitization of Usernames

WordPress cleans up usernames with a sanitization function named sanitize_user(). With the filter with the same name, you can customize this function.

Example: Accept Usernames Only in Lowercase

If you don't want to accept usernames with capital letters in it (whether it be "SHOUTINGBOY88" or "CrazyGirl92"), you can use PHP's strtolower function and hook it to the sanitize_user filter like below:

Filtering the Content of the Post

This one doesn't need much introduction: The the_content filter allows us to tamper with the post content.

Example: Removing the <p> Tags Wrapping Images

WordPress doesn't let us display images outside of a paragraph – it automatically wraps them a <p> tag, which is annoying for me. If you're annoyed with it as well, you can remove the <p> tags with the help of this little code snippet: