The myCRED Hook API can help you connect your favorite WordPress plugin with myCRED by setting up custom instances where points are awarded to a user for a certain action.
In order for us to detect a specific instance, we often need to “hook” into a third-party plugin or a specific WordPress instance via a action or filter hook that WordPress or the third party plugin provides (if they do). You do not have to use the Hook API to connect your plugin if you don’t want to, the API only serves to make hook creations as easy as possible.
The Hook Module
All hooks in myCRED are managed and controlled by the Hook Module. The module has two main jobs:
- Load active hooks.
- Provide the Hooks admin page where you can manage your hooks and hook settings.
The module will save the hooks you have installed, active / disabled and their settings for each point type. It will also load active hooks for each point type. A hook can be enabled for one type but not for another, it is up to you.
All myCRED hooks must be registered using the mycred_setup_hooks filter. You can use this filter to add your own hook, replace an existing one or remove hooks all together.
Example showing how you can use the mycred_setup_hooks filter to adjust myCRED Hooks.
Once a hook is registered, it needs to be loaded. To make this process as easy as possible, myCRED provides you with the mycred_load_hooks action hook. It is highly recommended that you use this action to load the hook class you created. Using this hook will ensure that the abstract myCRED_Hook class is available when needed.
Example showing how you can use the mycred_load_hooks action to load your custom myCRED Hook class.
The Hooks Class
To make hook construction as easy as possible, myCRED hooks are build of the myCRED_Hook abstract class. The hook class consists of the class constructor and the run method. If the hook needs settings, the preferences and sanitise_preferences class methods can be used to automatically insert settings on the hook page. myCRED Hook classes are similar to WordPress widget classes, in the way they are constructed.
For examples of how to build a custom hook, please have a look at the myCRED Hooks 101 tutorial.
myCRED Hook class skeleton: