Why the hooks?
Given some interest in extending our plugin we’ve decided to list the custom plugin available filters. We’ll be covering the fact on how to get rid of the default filtering and how to create custom filtering functions that may extend the plugin limitlessly.
There are four custom filters that may customize ACF Recent Posts functionality and the tutorial covers the usage of three of thees.
The hooks explained
This filter is used to add extra functionality to the area displayed before each post. Its default functionality gives one the ability to use custom meta and ACF fields assigned to each post. This filter runs only if there’s any content specified inside the textarea that corresponds with it.
To remove the default hook attached or to create a custom snippet (say to print out extra copyright text, or add custom shortcode), one can place the following code inside the custom plugin or within theme’s functions.php file:
One can use this hook to introduce new shortcode or HTML wrappers. ACF_Helper class contains all of the function definitions and corresponding regular expressions that introduce the default functionality,that is and [meta] tags.
The hook works in the exact same way as the hook above and has the same default hooks attached to it.
This filter is fired after the whole loop arguments have been constructed. One can use it to add some generic argument to each of the widgets or simply write custom complex queries which are not covered by the plugin.
The following example will modify each query and select only these posts that come from the same author.
The following hook can modify the plugin query meta value parameter provided that it was split into array. See plugin description and source code to figure out it’s behavior. The meta value query parameter can be also modified with the hook above though. This filter runs only if there are certain conditions met.
Summary and references
The tutorial is connected with the following plugin: https://wp-doin.com/portfolio/acfrpw which can be found here: https://wordpress.org/plugins/acf-recent-posts-widget/
Filter explanation: https://codex.wordpress.org/Function_Reference/add_filter
Latest posts by Rafał Gicgier (see all)
- WordPress application development ACF & Gravity Forms - 3 August 2019
- OptiPic.io – How and why to optimize the image on the site? - 8 April 2019
- Guest post at WP doin! - 26 March 2019