Please disable ad blocker to see the page.

Easiness Of Wordpress Filters And Their Usage

Filters are another segment of WordPress hook's story. As I have mentioned previously they are used to reform the output and now we will move forward from there.
So, look at the following functions, the story revolves around these four:
  1. add_filter()
  2. remove_filter()
  3. apply_filters()
  4. has_filter()
For your convenience the list of few filters are below:
  • bloginfo
  • body_class
  • check_password
  • date_formats
  • excerpt_length
  • gettext
  • get_category
  • import_upload_size_limit
  • image_resize_dimensions
  • login_body_class
  • mod_rewrite_rules
  • post_thumbnail_size
  • the_content
Find a nice list of all at adambrown.info.

Let's examine, how can you use filter hooks for your requirement?

To learn this, I am taking an example of featured image. In twenty twelve theme it shows above the title but if you wish to move its position to under that, you can do it with the help of filter. Look at the below procedure.

Create a function.

 function image_placing( $article)
 {
    if ( is_singular('post') && has_post_thumbnail())
 {
        $thumbnail = get_the_post_thumbnail();
        $arg= $thumbnail . $article;
 }
 return $arg;
 }

Now join this function with "the_content".

 add_filter( 'the_content', 'image_placing' );

Here you will see two images on the single post page.

In this situation, you may exclude the default image by removing "the_post_thumbnail()" from template.

Now its turn to understand the remove_filter, look at the syntax:

 remove_filter( 'the_content', 'image_placing');

By the above, it should be clear that it is used to remove any existing functionality from the filter.

Now what is apply_filters. It is same as do_action, set eyes on the below statement's explanation:

 apply_filters(
    $filtername,     // for add_filter;
    $valuetochange, //  variable whose value you can change
    $context_1,       // case
    $context_2        // more
 );

A simple example

 apply_filters('New_hook','Hi');  It will return Hi

Another with add_filter:

 add_filter( 'New_hook', 'my_hello' );
 apply_filters('New_hook','Hi'); it will pass Hi to my_hello function.

Let's use apply_filter for your theme.

Create a function as below. It will show the title in uppercase.

 function new_title()
 {
   $article= get_post( $post );
   $title = isset( $article->post_title ) ? $article->post_title : '';
   $textinuppercase=strtoupper($title);
  return apply_filters('new_title', $textinuppercase);
 }


Now you have to echo new_title() in your template file where you want to display the title. It should be displayed like this:


So this is a new filter and in any case you want to modify this output, just follow the below process. Here I will change it to lowercase.

Another new function to customize the output

 function lowercase_title()
 {
  $post = get_post( $post );
  $title =  $post->post_title;
  return $lowercase=strtolower($title);
 }

Then link this with new_title:

 add_filter('new_title', 'lowercase_title');

Now it should be like this:


And the last but not least the has_filter. It check the presence of any already added function in filter. For instance.

 if (!has_filter('the_content', 'image_placing'))
 {
    add_filter('the_content', 'customize_format');
 }

In the above instance, has_filter search for image_placing() and as in the condition if the function is not found then new function will be added to "the_content". It is nothing more than a conditional tag.

As I have explained all these things in approachable manner and hopefully you will be able to use them according to your desire. There are more hooks help to customize your child theme and plugin. We will discuss them later.
Previous
Next Post »
0 Comment