Javascript Hooks

To register javascript hooks, use the WPD.Hooks javascript object methods addFilter and removeFilter

/**
 * Adds a callback function to a specific programmatically triggered tag (hook)
 *
 * @param tag - the hook name
 * @param callback - the callback function variable name
 * @param priority - (optional) default=10
 * @param scope - (optional) function scope. When a function is executed within an object scope, the object variable should be passed.
 */
WPD.Hooks.addFilter = function( tag, callback, priority, scope )

/**
 * Removes a callback function from a hook
 *
 * @param tag - the hook name
 * @param callback - the callback function variable
 */
WPD.Hooks.removeFilter= function( tag, callback )

Usage

// It's best to hook to the load event to make sure WPD global is loaded
window.addEventListener("load", () => {
    // Add regular function
    let func1 = funtion(arg1, arg2) {
        return 'my value';
    }
    WPD.Hooks.addFilter('hook_name', func1);
    
    // Add an object method, with reference to "this" inside the method
    let myObject = {
        'myValue': 'my value',
        'myFunction': function(arg1, arg2) {
           return this.myValue;
        }
    };
    WPD.Hooks.addFilter('hook_name', myObject.myFunction, 10, myObject);
    
    // Anonymous function
    WPD.Hooks.addFilter('hook_name', function(arg1, arg2){
    	return 'my value';
    });
    
    // To remove all hooks
    WPD.Hooks.removeFilter('hook_name');
});

List of Hooks

Last updated