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:
<?php add_filter( 'gettext', 'gettext_example', 20, 3 ); function gettext_example( $translated_text, $text, $domain ) { switch ( $translated_text ) { case 'E-meil Adress' : $translated_text = __( 'Email Address', 'plugin_text_domain' ); break; } return $translated_text } // Example source: http://speakinginbytes.com/2013/10/gettext-filter-wordpress/ ?>
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:
<?php add_filter( 'sanitize_title', 'sanitize_title_example' ); function sanitize_title_example( $title ) { $title = str_replace( '-the-', '-', $title ); $title = preg_replace( '/^the-/', '', $title ); return $title; } ?>
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:
<?php add_filter( 'no_texturize_shortcodes', 'no_texturize_shortcodes_example' ); function no_texturize_shortcodes_example( $shortcodes ) { $shortcodes[] = 'myshortcode'; return $shortcodes; } // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/no_texturize_shortcodes ?>
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)":
<?php add_filter( 'pre_comment_approved', 'pre_comment_approved_example', 99, 2 ); function pre_comment_approved_example( $approved, $commentdata ) { return ( strlen( $commentdata['comment_author'] ) > 75 ) ? 'spam' : $approved; } // Example source: https://gist.github.com/norcross/5468979 ?>
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:
<?php add_filter( 'enable_post_by_email_configuration', '__return_false', 100 ); ?>
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:
<?php add_filter( 'enable_post_by_email_configuration', '__return_true', 100 ); ?>
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.
<?php add_filter( 'wp_title', 'wp_title_example', 10, 2 ); function wp_title_example( $title, $sep ) { global $paged, $page; if ( is_feed() ) return $title; // Add the site name. $title .= get_bloginfo( 'name' ); // Add the site description for the home/front page. $site_description = get_bloginfo( 'description', 'display' ); if ( $site_description && ( is_home() || is_front_page() ) ) $title = "$title $sep $site_description"; // Add a page number if necessary. if ( $paged >= 2 || $page >= 2 ) $title = sprintf( __( 'Page %s', 'tuts_filter_example' ), max( $paged, $page ) ) . " $sep $title"; return $title; } // Example source: http://tommcfarlin.com/filter-wp-title/ ?>
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:
<?php add_filter( 'preprocess_comment', 'preprocess_comment_example' ); function preprocess_comment_example( $commentdata ) { if( $commentdata['comment_content'] == strtoupper( $commentdata['comment_content'] )) $commentdata['comment_content'] = strtolower( $commentdata['comment_content'] ); return $commentdata; } // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/preprocess_comment ?>
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:
<?php add_filter( 'login_redirect', 'login_redirect_example', 10, 3 ); function login_redirect_example( $redirect_to, $request, $user ) { global $user; if ( isset( $user->roles ) && is_array( $user->roles ) ) { if ( in_array( 'subscriber', $user->roles ) ) { return home_url(); } else { return $redirect_to; } } return; } ?>
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:
<?php add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'plugin_action_links_example' ); function plugin_action_links_example( $links ) { $links[] = '<a href="' . get_admin_url( null, 'options-general.php?page=my_plugin_settings' ) . '">' . __( 'Settings' ) . '</a>'; return $links; } // Example source: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name) ?>
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:
<?php add_filter( 'the_editor_content', 'the_editor_content_example' ); function the_editor_content_example( $content ) { // Only return the filtered content if it's empty if ( empty( $content ) ) { $template = 'Hey! Don\'t forget to...' . "\n\n"; $template .= '<ul><li>Come up with good tags for the post,</li><li>Set the publish time to 08:00 tomorrow morning,</li><li>Change the slug to a SEO-friendly slug,</li><li>And delete this text, hehe.</li></ul>' . "\n\n"; $template .= 'Bye!'; return $template; } else return $content; } // Example source: http://wpfilte.rs/the_editor_content/ ?>
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!
Comments