Advertisement
  1. Code
  2. WordPress
Code

50 Filters of WordPress: Filters 21-30

by
Difficulty:BeginnerLength:MediumLanguages:
This post is part of a series called 50 Filters of WordPress.
50 Filters of WordPress: Filters 11-20
50 Filters of WordPress: Filters 31-40

In the previous parts of this series, we went through 30 WordPress filters. In this tutorial, we're going to see another batch of them and do examples with each.

Let's begin!

Filtering the Search Query

Searching is one of the most important features of WordPress (and it has stayed kind of primitive, for some reason). With the posts_search filter, you can monkey with the search SQL.

Example: Including Password Protected Posts in Search Results

Let's say you use posts for your clients' information and you set passwords for each post and send the password for your client. 

To let the clients search amongst the posts and find their own page, you can use the code snippet below to enable visitors find password-protected posts within the search results:

If the users of your blog are registered, you don't have to use the code above: They can search through password-protected posts as long as they're logged in – but of course, they still can't see the content unless they enter the password.

Setting Compression Quality for Uploaded Images

By default, WordPress tries to compress your images while resizing for the defined image sizes. With the wp_editor_set_quality filter, you can change or disable the compression rate.

Example: Prevent WordPress from Compressing & Distorting Your Images

Image compression in PHP results in subtle distortions which can only be seen by keen eyes for details. If your website have an sharp-eyed audience (Designers, maybe?) and don't want WordPress to use this compression algorithm, you can use the code below to disable compression and avoid distortion:

Filtering the Text Widget

Default widgets in WordPress has their own filters. The widget_text filter is the filter of, you guessed it, the Text widget.

Example: Enable Shortcodes for the Text Widget

It's kind of annoying that WordPress doesn't let us use shortcodes in the text widget by default. Thanks for its filter, though, we can enable shortcodes within the widget:

...wait, no function to hook? Actually, there is: It's a core function named do_shortcode() which you may have heard of. Since there's already a function to hook to the filter, we don't have to write another one.

Filtering the Feed Content

As you can filter the post content with the_content, you can tamper with the feed items, too, with the the_content_feed filter.

Example: Inserting Post Thumbnails to Feed Items

This example is one of my favorites among all the examples in this series: Adding the much-needed post thumbnail to your feed items!

If you want the thumbnails to be displayed on the right of the excerpt, simply change 'float:left;margin-right:.75em;' with 'float:right;margin-left:.75em;'.

Changing Buttons from the Visual Editor

WordPress comes with TinyMCE, an advanced WYSIWYG editor that we use while we write our posts. With the mce_buttons filter, we can change the first line of buttons in that editor.

Example: Remove Unwanted Buttons from the Visual Editor

If you run a multi-author blog and you don't want your authors to use specific buttons in the visual editor, you can remove some of the buttons (or all of them) with the code below:

Note that it doesn't disable the usage of the things you remove – it just removes the buttons.

As I said, this filter deals with the first line of the buttons in the visual editor. For the second line (the "advanced buttons"), there is another filter named mce_buttons_2 and there are two additional filters for two emtpy lines: mce_buttons_3 and mce_buttons_4.

Excluding Terms from Lists

There might be times when you need to "exclude" a term from every single term list in your website. This little filter allows us to do exactly what its name suggests.

Example: Excluding the Categories Your Plugin Created

Imagine you built a plugin that needs to create two categories named "Favorited-MyPlugin" and "Hated-MyPlugin". It would look ugly if the tags were listed in the categories lists, so you need to hide them from the lists. Here's how you do it:

Here's an interesting feature: This filter also hides the terms from your back-end lists. Cool... and creepy.

Changing the Image Sizes Dropdown

If you pick an image to use in your posts, you get to choose a size before adding it to the post content. This handy filter allows us to add custom image sizes (created with the add_image_size() function beforehand) to the dropdown menu where we select sizes.

Example: Letting Writers Choose Custom Image Sizes

Suppose you already created a custom image with a name of "golden-ratio-thumb" and you need your authors to add images with that size into their posts when necessary. Here's how you add the custom image size to the <select> menu:

It's almost too easy, right?

Changing the "More" String on Excerpts

WordPress cuts off the first 55 words of your posts, removes formatting and calls them "excerpts", summaries of the posts. At the end of the excerpts, there is a [...] string which is kind of annoying for me. Luckily, the excerpt_more filter allows us to change it.

Example: Adding a Link to the Post After the Excerpt

Let's say you want to change that text and turn it into a link to the post. Here's how you do it:

So now, that annoying bit of text has been put to good use.

Managing Columns in the Posts List

On the Posts page in your admin panel, you can see the list of your posts as a table with additional information like authors, categories, tags of the posts. 

The manage_posts_columns allows us to hide them or add new ones (by using actions and action functions).

Example: Remove the "Author" Column

If you're the only author in your website, you don't need to see the author of each post; so you might as well remove that column to save space. Here's how you do it:

There you go! By default, there are seven columns which you can hide:

  • cb (the checkboxes)
  • title
  • author
  • categories
  • tags
  • comments
  • date

To make it work in custom post types, you can use the manage_$post_type_posts_columns where $post_type is the ID name of your custom post type.

Editing Users' Contact Methods

Did you know that you can add (or remove) fields in user profiles? Yep, that's what the user_contactmethods filter is for.

Example: Adding User Fields for Social Media Accounts

This is kind of a popular code snippet because WordPress—still—has profile fields like "Jabber" and "AIM" and people want to get rid of them. In this example, we're going to delete some default "contact methods" and add a few new profile fields:

The profile fields look more like 2014 and less like 2002 now, right?

End of Part Three

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!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.