Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. WordPress

50 Filters of WordPress: Filters 11-20

Read Time: 8 mins
This post is part of a series called 50 Filters of WordPress.
50 Filters of WordPress: The First 10 Filters
50 Filters of WordPress: Filters 21-30

In the previous part of this series, we started going through 50 WordPress filters. Selected among hundreds, we started by reviewing 10 of them.

In this tutorial, we're going to see another batch of them and review some examples of each.

Let's begin!

Playing With Translatable Data in WordPress

One of the coolest features of WordPress is having every bit of text ready to be translated. If your website's language is English, however, you don't need this feature - or do you? 

The gettext filter might work for you in a different way. Let's see an interesting example.

Example: Correct Another Developer's Grammar!

Suppose you found a nice plugin to work with, but you realize that its owner doesn't speak English very well, and you see some poorly written text inside the code. Luckily the strings are translatable, so you're going to be able to change those strings with the help of the gettext filter. 

Let's see how:

Cleaning Up the Slug

WordPress uses a function named sanitize_title() to clean up the titles, replaces spaces with hyphens and makes them ready to save as slugs. With the filter sanitize_title (yep, same name) you can extend this function.

Example: Removing the "The" Words from the Slug

If you don't want the word "the" in your slugs, you can delete them with the code snippet below:

A simple and elegant solution.

Setting Exceptions for Shortcode Texturization

This handy filter "allows you to specify which shortcodes should not be run through the wptexturize() function", as said in the Codex.

Example: Exclude Your Shortcode from Texturization

If you want the shortcode you built to exempt from texturization, use this code to add your shortcode name to the "do not texturize" list:

Pretty easy, right?

Filtering a Comment's Approval Status

WordPress has its own checks for comments (which may be a little too easy against spammers) before deciding whether the comment should be marked as spam, be sent to the moderation queue or be approved. The pre_comment_approve filter lets plugins help with this decision.

Example: Marking Comments With Long Author Names as Spam

In my country, Turkey, WordPress comment spammers usually use reeeaaally long names, sometimes the URL itself. 

With the code snippet below, you can automatically eliminate spammers who use names like "Domestic and International Shipping With Extremely Low Prices (Click Here for More Information)":

Special thanks to Andrew Norcross for the idea!

Bonus tip: If you want to eliminate spam by checking the length of the comment author's URL, use 'comment_author_url' instead of 'comment_author'. Andrew Norcross used the URL in his original tip, by the way.

Configuring the "Post By Email" Feature

Did you know that you can post to your WordPress blog via email? WordPress offers this seldom used feature and it allows you to turn it on or off with the enable_post_by_email_configuration filter.

Example: Turning the "Post By Email" Feature On and Off

For some reason (such as security, maybe) you might want to turn off this feature. And you can do it with just one line of code:

Or if you're on WordPress Multisite and you need to enable this feature (since it's disabled by default on Multisite), you can use the __return_true() function:

Filtering Your Page Titles

The wp_title() function outputs the page titles, the ones that we see on our tab handles in browsers. And the wp_title function allows us to tamper with those titles.

Example: Rewriting Your Page Titles – The Right Way

A respected WordPress "guru" (and editor at Tuts+ Code) Tom McFarlin explains us in his blog how to rewrite our page titles properly with the wp_title() function and the filter with the same name:

Since wp_title is a filtered function, this means that we're able to provide a custom hook that allows us to define the schema for displaying our titles not only more precisely, but also correctly.

Be sure to check out his article. Thanks, Tom!

Processing Comments Before They're Saved to the Database

If you need any help changing the comments' data (the post ID of the comment, author's name, author's email address, author's website, type of the comment, user's ID if the commenter is a user, type of the comment and comment content), preprocess_comment can help you.

Example: Turn Down the Volume of Yellers

DO YOU GET A LOT OF COMMENTS IN WHICH EVERY SINGLE WORD IS UPPERCASE? If you do, you can automatically make those letters lowercase with the code snippet below:

Cool, huh?

Managing Redirection After Login

This little filter allows us to set redirects (other than the administration panel) following the login process, which can be pretty useful in some cases.

Example: Redirect Subscribers to Website Home

If you don't want your users (with the role "Subscriber") to see your admin panel after they login, you can redirect them to your website's homepage:

The Codex warns us about one thing: "Make sure you use add_filter outside of is_admin(), since that function is not available when the filter is called."

Creating Action Links for Your Plugin

If you're developing a plugin, you might wonder how other developers have managed to add links under their plugins' names in the Plugins page. Well, they use this filter.

Example: Adding a "Settings" Link to Display in the Plugins Page

To add custom action links under your plugin's name at the list in the Plugin page, you can use this function and hook it to the filter:

Notice that we're using the __FILE__ constant to hook our function to the filter with your plugin's name. Neat, huh?

Use this with caution: If you abuse that area to fill there with links, people will remember you as a spammer.

Filtering the Content Inside the Post Editor

Ever wanted to pre-fill the post editor to start writing with a post template, or leaving notes for your authors? You can, thanks to the the_editor_content filter.

Example: Leaving Reminders for Your Authors

Let's do the "leaving notes for authors" example: If you have a bunch of things to remind the writers of your blog, you can fill the post editor with HTML by using this code:

Change the $template variable with anything you like and you're good to go!

End of Part Two

We went through the second batch of 50 filters in this article. I hope you liked and learned new things from it. See you in the next one!

I want to hear your thoughts, too. What do you think about these filters? Post your comments below; and if you liked the article, don't forget to share it!

Did you find this post useful?
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.