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:
<?php add_filter( 'use_default_gallery_style', '__return_false' ); ?>
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:
<?php add_filter( 'wp_get_attachment_url', 'wp_get_attachment_url_example' ); function wp_get_attachment_url_example( $url ) { $http = site_url( false, 'http' ); $https = site_url( false, 'https' ); if ( $_SERVER['HTTPS'] == 'on' ) return str_replace( $http, $https, $url ); else return $url; } // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_get_attachment_url ?>
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:
<?php add_filter( 'wp_mail_content_type', 'wp_mail_content_type_example' ); function wp_mail_content_type_example( $content_type ) { return 'text/html'; } ?>
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:
<?php add_filter( 'pre_comment_user_ip', 'pre_comment_user_ip_example' ); function pre_comment_user_ip_example() { $REMOTE_ADDR = $_SERVER['REMOTE_ADDR']; if ( !empty( $_SERVER['X_FORWARDED_FOR'] ) ) { $X_FORWARDED_FOR = explode( ',', $_SERVER['X_FORWARDED_FOR'] ); if ( !empty( $X_FORWARDED_FOR ) ) $REMOTE_ADDR = trim( $X_FORWARDED_FOR[0] ); } elseif( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { $HTTP_X_FORWARDED_FOR = explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ); if ( !empty( $HTTP_X_FORWARDED_FOR ) ) $REMOTE_ADDR = trim( $HTTP_X_FORWARDED_FOR[0] ); } return preg_replace( '/[^0-9a-f:\., ]/si', '', $REMOTE_ADDR ); } // Example source: https://codex.wordpress.org/Plugin_API/Filter_Reference/pre_comment_user_ip ?>
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:
<?php add_filter( 'wp_revisions_to_keep', 'wp_revisions_to_keep_example', 10, 2 ); function wp_revisions_to_keep_example( $num, $post ) { if ( 'event' == $post->post_type ) { return 0; } return $num; } // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_revisions_to_keep ?>
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:
<?php add_filter( 'img_caption_shortcode', 'img_caption_shortcode_example', 10, 3 ); function img_caption_shortcode_example( $empty, $attr, $content ) { $attr = shortcode_atts( array( 'id' => '', 'align' => 'alignnone', 'width' => '', 'caption' => '' ), $attr ); if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) { return ''; } if ( $attr['id'] ) { $attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" '; } $figure_atts = $attr['id'] . ' class="caption ' . esc_attr( $attr['align'] ) . '" ' . 'style="max-width: ' . ( 10 + (int) $attr['width'] ) . 'px;"'; $output = '<figure ' . $figure_atts . '>'; $output .= do_shortcode( $content ); $output .= '<figcaption>' . $attr['caption'] . '</figcaption>'; $output .= '</figure>'; return $output; } // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/img_caption_shortcode ?>
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:
<?php add_filter( 'post_class', 'post_class_example' ); function post_class_example( $classes ) { global $wp_query; if ( 0 == $wp_query->current_post ) { $classes[] = 'first-post'; } return $classes; } // Example source: http://www.billerickson.net/code/first-post-class/ ?>
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:
<?php add_filter( 'attachment_fields_to_edit', 'attachment_fields_to_edit_example', 10, 2 ); function attachment_fields_to_edit_example( $form_fields, $post ) { $field_value = get_post_meta( $post->ID, 'license', true ); $form_fields['license'] = array( 'value' => $field_value ? $field_value : '', 'label' => __( 'License' ), 'helps' => __( 'Specify the license type used for this image' ) ); return $form_fields; } add_action( 'edit_attachment', 'save_new_attachment_field' ); function save_new_attachment_field( $attachment_id ) { $license = $_REQUEST['attachments'][$attachment_id]['license']; if ( isset( $license ) ) { update_post_meta( $attachment_id, 'license', $license ); } } // Example source: http://codex.wordpress.org/Plugin_API/Filter_Reference/attachment_fields_to_edit ?>
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:
<?php add_filter( 'excerpt_length', 'excerpt_length_example' ); function excerpt_length_example( $words ) { return 15; } ?>
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:
<?php add_filter( 'bulk_actions-edit-post', 'bulk_actions_edit_post_example' ); function bulk_actions_edit_post_example( $actions ) { unset( $actions['trash'] ); return $actions; } ?>
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!
Comments