Wordpress custom filter hook

This is just an example explaining how to create a wordpress custom filter hook

Let’s say you have a page or a plugin displaying posts/custom posts based on a certain criteria

Now you wish to provide a hook to the end user to be able to modify how the posts get displayed.

Here is a sample code to display posts using a standard wordpress loop

Note that we have displayed the  posts after getting the contents in a variable named $wpi_snippet_view. This helps us to define a hook to be able to modify this variable.

Below code shows how to define our custom filter hook

The only difference is the above code is line number 19 where we have defined the custom hook as wpi_snippet_view_hook

Now that the custom hook is defined, all we need to do is to call the hook in our functions.php and modify the variable $wpi_snippet_view to show the required output.

Using the above hook we have changed the thumbnail size from thumbnail to large. You can change the view completely as per your requirements. Using the above hook you can present dynamic posts in various ways on the same page.

Leave a Reply

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