50 Filters of WordPress: Filters 31-40

In the previous parts of this series, we went through 20 WordPress filters. In this tutorial, we're going to continue the trend and review another batch of them and follow along examples with each.

Let's begin!

Filtering the Default Gallery Style

WordPress styles the [gallery] shortcode with some pre-defined CSS. You can change this behavior with the use_default_gallery_style filter.

Example: Disable the Default CSS

If you already have styled your galleries in your theme's style.css file and don't need the default CSS rules, you can disable them by using the code below:

Done! Now WordPress won't add a <style> tag with a bunch of CSS rules before your [gallery] shortcodes.

Filtering the Attachment URLs

This filter applies to the attachment URL retreived by the function with the same name, wp_get_attachment_url().

Example: Avoiding "Mixed Content" Warnings

If you're using SSL encyription in your website, the wp_get_attachment_url() function may return a URL with HTTP instead of HTTPS, which results with a "mixed content" warning for visitors. With the function below, you can prevent this warning to be shown:

Setting the Default Content Type for Email

You can only send plain text emails in WordPress by default, since the wp_mail() function uses text/plain as content type. With the wp_mail_content_type filter, you can change that.

Example: Send Emails With WordPress Using HTML

If you want to be able to send HTML emails, you can use the code snippet below to change WordPress' content type for emails:

Saving the IP Address of the Commenter

WordPress saves the IP address of each commenter in the comments table of your database. If you want to tamper with them, you can use the pre_comment_user_ip filter.

Example: Saving the Real IP Address of the Commenter

If a commenter uses a proxy server to submit a comment, WordPress will record the proxy server's IP address instead of the commenter's real IP. The real IP is served with another HTTP header, X-Forwarded-For. The code snippet below has the function to take that record, extract the real IP address and save it in your database:

In case you ever wonder about the X-Forwarded-For HTTP header, visit Wikipedia for additional information.

Changing the Number of Revisions to Save for Posts

You probably know WordPress has a feature that lets us keep "revisions" of our posts, and maybe you also know that you can change how many revisions to keep by adding the constant (WP_POST_REVISIONS) into the wp-config.php file. 

But did you know that you can change the number of revisions for different post types, even for different posts? Yep, that's what the wp_revisions_to_keep filter is for.

Example: Disabling Revisions for a Specific Custom Post Type

You can basically change how many revisions to keep by playing with the $post variable, but we're going to keep things simple and limit the number of revisions for a custom post type named "event" in this example:

You can see a quick reference for the $post variable in the Codex to find new ways to use this filter. Don't forget to comment and share your idea with us, if you have a good one!

Rewriting the [caption] Shortcode

The [caption] shortcode simply lets you wrap your images with captions. If you ever need to change the output of the shortcode, you can use this filter.

Example: Image Captions With HTML5 Markup

HTML5 introduces us with two new image-related tags: <figure> and <figcaption>. In this example, we're going to rewrite the markup of the [caption] with these tags:

Play with the code as you like – you might not like to use IDs for the <figure> tag, for example. Other than that, it work out of the box!

Adding Post Classes

The body_class filter (and the function) is recognizable among WordPress developers. How about post_class, did you know that there was a filter (and a function) for adding classes to post containers?

Example: Adding a Special Class for the First Post in the Loop

A perfect example would be adding a custom class just for the first post in the Loop. Let's see how:

From now on, you don't have to use the :first pseudo-class in CSS and worry about cross-browser compatibility – just use the .first-post class!

Adding Custom Fields for Attachments

In WordPress, attachments are essentially very similar to custom post types that can be extended like any other post type – just a bit more differently. The filter attachment_fields_to_edit lets us play with the fields when we upload or edit attachments.

Example: Adding License Information to Your Uploaded Images

Let's say you have a photography-related blog and you need to set licenses for every single photo that you upload as an attachment. With the help of "actions" this time, we can add a "License" field for your attachments:

An amazing example how we can extend core WordPress functions without editing any code in core.

Changing the Automatic Excerpt Length

By default, automatic excerpts are made by cutting of 55 words of each post and remove their formatting. An odd number, if you ask me. If you want to change it, the excerpt_length filter allows you to do it.

Example: Change the Automatic Excerpt Length

Let's say I want only 15 words because I'm doing a Pinterest-style layout and I don't have much space for summaries. All I need to do is return the number in a function and hook the function to this tiny filter:

Easy, right?

Playing With Bulk Actions in Admin Screens

Right above the item lists in the WordPress admin pages (like Posts, Pages, Users, Media and so on), there are some "bulk actions" where you select a bunch of items and do things. This handly filter lets us monkey with that little dropdown menu.

Example: Disabling Bulk Post Trashing

You own a news website and you're sick of your editor accidentally trashing the posts he needs to update. If you don't even need the bulk-trash feature, you can turn it off:

The $screenid variable in the filter name is the screen name of the admin page you're going to change. You can find a list of these screen names on the "Admin Screen Reference @ Plugin API" page of Codex.

End of Part Four

We went through the third 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!

Tags:

Comments

Related Articles