Action Hooks & Filters
Overview
WordPress allows using action hooks and filters to change functionality of plugins and themes. You can read about hooks and filters more here.
Action Hooks
WP Reactions include some action hooks that can be useful for developers to extend functionality of plugin by using them. We have listed action hooks with their data and example below.
wpreactions/user/initialized
Fires just after plugin loads all of itse components and ready. This hook can be used to do some actions that needs to be ensure plugin is loaded and ready.
wpreactions/user/react
After user reacts to any of reactions, this hook is fired with the reacted emoji data. You can use this hook to do certain actions after user react, for example give them some points in your site.
Hook function receive 2 parameters:
array $data – holds information about user react
- bind_id – post id or unique id for standalone shortcodes
- react_id – unique id to identify user reaction
- reacted_date – date of reaction
- source – identify if reaction comes from global activation or shortcode
- emoji_id – id of emoji
- user_id – user id if user logged in, 0 (zero) if not logged in
- sgc_id – if source is shortcode, then this will set to shortcode id
- post_type – post type of the post user is reacting
array $response – holds information about reaction if it is success or fail
- action – if user react first time ‘insert’, otherwise ‘update’
- status – determine if reaction was ‘success’ or ‘error’
- error_message – in case of error, it holds information about error
add_action('wpreactions/user/react', function($data, $response) { if ($response['status'] == 'success' && $data['user_id'] == 1) { update_user_meta($data, 'user_bonus', 10); } });
wpreactions/user/socialShare
After user reacts to any of reactions, this hook is fired with the reacted emoji data. You can use this hook to do certain actions after user react, for example give them some points in your site.
Hook function receive 2 parameters:
array $data – holds information about user react
- bind_id – post id or unique id for standalone shortcodes
- platform – social platform that user shared to
- click_date – date of sharing
- source – identify if share comes from global activation or shortcode
- user_id – user id if user logged in, 0 (zero) if not logged in
- sgc_id – if source is shortcode, then this will set to shortcode id
- post_type – post type of the post user is sharing
array $response – holds information about sharing if it is success or fail
- action – if user react first time ‘insert’, otherwise ‘update’
- status – determine if reaction was ‘success’ or ‘error’
- error_message – in case of error, it holds information about error
If user share on Facebook, we mark it as facebook_user.
add_action('wpreactions/user/socialShare', function($data, $response) { if ($response['status'] == 'success' && $data['platform'] == 'facebook') { update_user_meta($data, 'facebook_user', 'yes'); } });
Filters
WP Reactions provides some filters that can be useful for developers to extend functionality of plugin by using them. We have listed filters with their data and example below.
wpreactions/output/params
Filters parameters of reactions just before rendered in frontend. You can change these params by using the filter and have customized look with some complex logic. There are lots of params that passed in $params array, you can just print it out and see full list in your code.
Example shows that how you can disable how you can disable social share buttons for pages.
add_filter('wpreactions/output/params', function($params) { global $post; if ($post->post_type == 'page') { $params['enable_share_buttons'] = 'false'; } return $params; });
wpreactions/output
Filters output HTML just before rendering it on frontend. You can use it to add some HTML before and after reactions based on your needs.
Example show that you can remove reactions for page id = 1 (front page) returning to empty string.
add_filter('wpreactions/output', function($html) { global $post; if ($post->ID == 1) { $html = ''; } return $html; });