WordPress custom hooks
What are WordPress hooks?
Hooks are provided by WordPress to allow your plugin to 'hook into' the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion. There are two kinds of hooks:
- Actions
- Filters
You can sometimes accomplish the same goal with either an action or a filter. For example, if you want your plugin to change the text of a post, you might add an action function to publish_post (so the post is modified as it is saved to the database), or a filter function to the_content (so the post is modified as it is displayed in the browser screen).
The Reviewer plugin offers some custom WordPress hooks. You can place the PHP code in the functions.php your WordPress theme or use the brand new Reviewer Extension, a companion plugin for storing all your extra code. Lore more in the related article.
Filters
rwp_before_saving_review
It will be applied before a new user review is saved on the database. It is useful for making some changes before the review will be stored. The callback accepts one argument: the review itself.
function rex_my_filter ($review) { // Filter the review before it is going to be saved. return $review; } add_filter('rwp_before_saving_review', 'rex_my_filter', 11, 1);
rwp_snippets
It will be applied before the Google Structured Data are added to the page content. It is useful if you want to customize the Google snippets; for example, changing the type of Schema or adding some missing fields. The callback accepts one argument: the snippets itself.
function rex_my_filter2 ($snippets) { // Filter the Google Structured data before they are going to be inserted. return $snippets; } add_filter('rwp_snippets', 'rex_my_filter2', 11, 1);
Actions
rwp_after_saving_review
It will be executed before the review process is complete, so after the review is stored in the database successfully. It accepts two arguments: the review itself and the post meta ID that identifies the row in the database.
function rex_my_action ($review, $post_id) { // Build your action after the review is saved. } add_action('rwp_after_saving_review', 'rex_my_action', 11, 2);