Docly Child

Hook API

Estimated reading: 5 minutes 528 views

Hook API

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:

  1. Load active hooks.
  2. 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.

Hook Registrations

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.

				
					add_filter( 'mycred_setup_hooks', 'mycredpro_adjust_hooks', 10, 2 );
function mycredpro_adjust_hooks( $installed, $point_type ) {

	// Remove a specific hook
	unset( $installed['site_visit'] );

	// Add a custom hook
	$installed['mycustomhook'] = array(
		'title'        => 'Hook Title',
		'description'  => 'Optional hook description. Must be defined but can be empty.',
		'callback'     => array( 'my_Custom_Hook_Class' )
	);

	// Replace an existing hook with our own
	$installed['site_visit'] = array(
		'title'        => 'Custom Site Visit',
		'description'  => 'A custom version of the built-in site visit hook.',
		'callback'     => array( 'my_Custom_Version_Hook_Class' )
	);

	return $installed;

}
				
			

Load Hook

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.

				
					add_action( 'mycred_load_hooks', 'mycredpro_load_custom_hook' );
function mycredpro_load_custom_hook() {

	// Require file containing the class or
	// define the class in this function
	class my_Custom_myCRED_Hook extends myCRED_Hook {

		// ...

	}

}
				
			

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:

 

				
					/**
 * myCRED Hook Skeleton
 * @version 1.2
 */
class my_custom_hook_class extends myCRED_Hook {

	/**
	 * Construct
	 * Used to set the hook id and default settings.
	 */
	function __construct( $hook_prefs, $type ) {

		parent::__construct( array(
			'id'       => 'unique_hook_id',
			'defaults' => array(
				'creds'   => 1,
				'log'     => 'points for something'
			)
		), $hook_prefs, $type );

	}

	/**
	 * Run
	 * Fires by myCRED when the hook is loaded.
	 * Used to hook into any instance needed for this hook
	 * to work.
	 */
	public function run() {

	}

	/**
	 * Hook Settings
	 * Needs to be set if the hook has settings.
	 */
	 public function preferences() {

		// Our settings are available under $this->prefs
		$prefs = $this->prefs;

	}

	/**
	 * Sanitize Preferences
	 * If the hook has settings, this method must be used
	 * to sanitize / parsing of settings.
	 */
	public function sanitise_preferences( $data ) {

		return $data;

	}

}
				
			

Previously

Next up

Share this Doc
CONTENTS