How to use Filter Hooks in WordPress





In the previous section, we learned through a common type of hook in WordPress, Action Hook. But besides Action Hook, we have another type of hook that is also used very often and plays an equally important role when programming in WordPress, which is Filter Hook.

What are Filter Hooks?

Filter Hook means an anchor point declared in the WordPress source code, plugin or theme so that we can modify the PHP script where that anchor point was declared.

How to use Filter Hook

Filter Hook is declared by function apply_filters() like this (can be tried in the footer.php template):


    <?php
        $copyright = ‘Design by ThachPham’;
        echo apply_filters( ‘thachpham_copyright’, $copyright );
    ?>

As you can see, instead of echoing the variable $copyright then I will put this variable in the function apply_filters()in there thachpham_copyright is the name of my Filter Hook.

How to use Filter Hooks in WordPress 15

Now I want it to not echo $copyright but it will be another variable due to self-resetting, we do not need to edit the above code directly, but can create a callback function and then use the function add_filter() to call it. And again, this is not a template tag that should be included in the functions.php of your theme or plugin.


function thachpham_change_copyright( $output ) {
    $output = ‘Design by WordPress’;
    return $output;
}
add_filter( ‘thachpham_copyright’, ‘thachpham_change_copyright’ );

Now you see, the line Design by ThachPham has been replaced with the new data string Design by WordPress through filtering.

See more:  Common Functions and Objects in WordPress

This filter is simply we declare any parameter in the callback function, it will interpret this as a function containing data, then in the function we assign it a new value and return about, finally use add_filter() hook into the filter hook that needs to be changed.

Some examples of Filter Hooks in WordPress

Just like Action Hooks, WordPress has several filter hooks available that you can use. Here I will go through some typical filter hooks for you to practice.

How to use Filter Hooks in WordPress 15

the_content

This hook will help us filter the content displayed to the outside. Let’s try to apply it to the bold of a certain keyword by combining the str_replace() function in PHP.


function thachpham_content_filter( $content ) {
    $find = ‘hello’;
    $replacement = "<strong>hello</strong>";
    $content = str_replace( $find, $replacement, $content );
    return $content;
}
add_filter( ‘the_content’, ‘thachpham_content_filter’ );

wp_handle_upload_prefilter

This hook will be called to filter files uploaded to the WordPress Media Library. Based on this hook, you can use the $file parameter to rename the file after uploading to the server.


<?php
add_filter(‘wp_handle_upload_prefilter’, ‘custom_upload_filter’ );

function custom_upload_filter( $file ){
    $file[‘name’] = ‘wordpress-is-awesome-‘ . $file[‘name’];
    return $file;
}

?>

How to use Filter Hooks in WordPress 15

Epilogue

Simple, isn’t it? Just need to read a little bit carefully and diligently read the code in WordPress, you can know how to use some of its built-in filters which are great for your purpose, it is as simple as Action Hook, not nothing. But from this simplicity, you can do a lot of things, from using some theme frameworks or writing your own plugins.


Source: How to use Filter Hooks in WordPress
– TechtipsnReview

, ,

Leave a Reply

Your email address will not be published. Required fields are marked *