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:
<?php add_filter( 'login_errors', 'login_errors_example' ); function login_errors_example( $error ) { $error = ''; return $error; } ?>
There. Much better.
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:
<?php add_filter( 'comment_post_redirect', 'comment_post_redirect_example' ); function comment_post_redirect_example( $location ) { return '/thanks-for-your-comment/'; } ?>
Note that WordPress uses the function wp_safe_redirect()
which means you must use a local page or a page in allowed hosts (see the allowed_redirect_hosts
filter below).
Allowing External Redirection for the wp_safe_redirect()
Function
By default, the wp_safe_redirect()
function doesn't allow external redirections. By using this filter, however, we can change that behavior and specify external hosts.
Example: Allowing Subdomain Redirections
Since the wp_safe_redirect()
function allows only redirections within your WordPress installation, subdomains of your domain are off limits, too.
Let's make WordPress allow them:
<?php add_filter( 'allowed_redirect_hosts', 'allowed_redirect_hosts_example' ); function allowed_redirect_hosts_example( $content ) { $content[] = 'forum.mywebsite.com'; $content[] = 'welcome.mywebsite.com'; $content[] = 'help.mywebsite.com'; return $content; } // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/allowed_redirect_hosts ?>
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.
Here's how:
<?php add_filter( 'body_class', 'body_class_example' ); function body_class_example( $classes ) { if( is_single() ) { foreach( get_the_category( get_the_ID() ) as $category ) $classes[] = 'cat-' . $category->category_nicename; } return $classes; } // Example source: https://codex.wordpress.org/Function_Reference/body_class#Add_Classes_By_Filters ?>
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:
<?php add_filter( 'locale', 'locale_example' ); function locale_example( $lang ) { if ( 'tr' == $_GET['language'] ) { return 'tr_TR'; } else { return $lang; } } // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/locale ?>
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:
<?php add_filter( 'sanitize_user', 'strtolower' ); // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/sanitize_user ?>
This is probably one the easiest filter examples in this series.
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:
<?php add_filter( 'the_content', 'the_content_example' ); function the_content_example( $content ) { return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content); } // Example source: http://wpsnipp.com/index.php/functions-php/remove-p-tag-from-around-images-in-the_content/ ?>
Filtering the Forms for Password Protected Posts
For password protected posts, WordPress automatically replaces the post content with a password form. With the the_password_form
filter, you can edit this form.
Example: Simplifying the Password Form
If password protected posts are a regular thing for your WordPress website and you don't need the "This content is password protected.
To view it please enter your password below:" text over an over again in loops, you can clean up and simplify the password form with the help of this code snippet:
<?php add_filter( 'the_password_form', 'the_password_form_example' ); function the_password_form_example() { $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">'; $output .= '<span>' . __( "Enter the password:" ) . ' </span>'; $output .= '<input name="post_password" type="password" size="20" />'; $output .= '<input type="submit" name="Submit" value="' . esc_attr__( "Go" ) . '" />'; $output .= '</form>'; return $output; } // Example source: http://codex.wordpress.org/Using_Password_Protection#Password_Form_Text ?>
There you go; now you only have three words, a password input field and a submit button, all in a single line.
Filtering the the_terms()
Function
If you're not satisfied with the output of the the_terms()
function, or you simply want to use it with a different purpose, you can use the filter with the same name: the_terms
.
Example: Strip HTML Tags from the the_terms()
Function
A few years ago, I needed to be able to have the tags of my posts in plain text. I spent many, many hours to find a solution – turns out there was an extremely simple one:
<?php add_filter( 'the_terms', 'strip_tags' ); ?>
You can't imagine the look of my face when I found out that I can simply hook a core PHP function to the the_terms
filter.
Changing the Email Address to Send From
When WordPress sends emails to you or your website's members, it uses an email address like [email protected]
. With this filter, you can change that.
Example: Setting Your Own Email Address as "From Email Address"
With a function just returning a different email address, you can change the sender's email address of emails:
<?php add_filter( 'wp_mail_from', 'wp_mail_from_example' ); function wp_mail_from_example( $email ) { return '[email protected]'; } ?>
You can use it along with the wp_mail_from_name
filter: Same logic, just write a function to return another string of a custom name and hook it to the wp_mail_from_name
filter.
Wrapping Up for Today
We went through 10 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!
Comments