Introduction

Juzaweb
Docs

About

Actions

Helper functions

/**
 * Do action hook
 * @param string $tag
 * @param mixed ...$args Additional parameters to pass to the callback functions.
 * @return void
 * */
function do_action($tag, ...$args)
/**
 * Add action to hook
 * @param string $tag The name of the filter to hook the $function_to_add callback to.
 * @param callable $callback The callback to be run when the filter is applied.
 * @param int $priority Optional. Used to specify the order in which the functions
 *                                  associated with a particular action are executed.
 *                                  Lower numbers correspond with earlier execution,
 *                                  and functions with the same priority are executed
 *                                  in the order in which they were added to the action. Default 20.
 * @param int $arguments Optional. The number of arguments the function accepts. Default 1.
 * @return void
 */
function add_action($tag, $callback, $priority = 20, $arguments = 1)

Example

do_action('my.hook', $user);

For example if you wanted to hook in to the above hook, you could do:

add_action('my.hook', function($user) {
    if ($user->is_awesome) {
         $this->doSomethingAwesome($user);
    }
}, 20, 1);

Filters

Helper functions

/**
 * Apply filters to value
 * @param string $tag The name of the filter hook.
 * @param mixed  $value The value to filter.
 * @param mixed  ...$args Additional parameters to pass to the callback functions.
 * @return mixed The filtered value after all hooked functions are applied to it.
 */
function apply_filters($tag, $value, ...$args)
/**
 * @param string $tag The name of the filter to hook the $function_to_add callback to.
 * @param callable $callback The callback to be run when the filter is applied.
 * @param int $priority Optional. Used to specify the order in which the functions
 *                                  associated with a particular action are executed.
 *                                  Lower numbers correspond with earlier execution,
 *                                  and functions with the same priority are executed
 *                                  in the order in which they were added to the action. Default 20.
 * @param int $arguments   Optional. The number of arguments the function accepts. Default 1.
 * @return bool
 */
function add_filters($tag, $callback, $priority = 20, $arguments = 1)

Example

$value = apply_filters('my.hook', 'awesome');
add_filters('my.hook', function($what) {
    $what = 'not '. $what;
    return $what;
}, 20, 1);
add_action('my.hook', function($what) {
    $what = add_filters('my.hook', 'awesome');
    echo 'You are '. $what;
});

Using in Blade

Adding the same action as the one in the action example above:

@do_action('my.hook', $user)

Adding the same filter as the one in the filter example above:

You are @apply_filters('my.hook', 'awesome')