PHP code example of lotharthesavior / hook

1. Go to this page and download the library: Download lotharthesavior/hook library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lotharthesavior / hook example snippets



use Hook\Filter;

Filter::addFilter('filter_name','filter_function');

function filter_function($content){
   return $content.'this came from a hooked function';
}



use Hook\Filter;

echo Filter::applyFilters('filter_name','this is the content: ');


use Hook\Action;

Action::addAction('header_action','echo_this_in_header');

function echo_this_in_header(){
   echo 'this came from a hooked function';
}



use Hooks\Action;

echo '<div id="extra_header">';
Action::doAction('header_action');
echo '</div>';

/**
 * Adds Hooks to a function or method to a specific filter action.
 *
 * @param string $tag The name of the filter to hook the {@link $functionToAdd} to.
 *
 * @param callable $functionToAdd The name of the function to be called 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 (default: 50).
 *                      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.
 *
 * @param ?string $false to check if it's true!
 *
 * @param string $tag The name of the filter hook.
 * @param false|callable $function_to_check [optional] Callback function name to check for
 *
 * @return string|int|bool If {@link $function_to_check} is omitted,
 *         returns boolean for whether the hook has
 *         anything registered.
 *         When checking a specific function, the priority
 *         of that hook is returned, or false if the
 *         function is not attached.
 *         When using the {@link $function_to_check} argument,
 *         this function may return a non-boolean value that
 *         evaluates to false (e.g.) 0, so use the === operator for testing the return value.
 *
 */
public static function hasFilter(string $tag, false|callable $function_to_check = false): string|int|bool

/**
 * Removes a function from a specified filter hook.
 *
 * @param string $tag The filter hook to which the function to be removed is hooked.
 * @param callable $functionToRemove The name of the function which should be removed.
 * @param int $priority [optional] The priority of the function (default: 50).
 *
 * @return bool
 */
public static function removeFilter(
    string $tag,
    callable $functionToRemove,
    int $priority = Hook\Constants::PRIORITY_NEUTRAL,
): bool

/**
 * Hooks a function on to a specific action.
 *
 * @param string $tag The name of the action to which the
 *                    $functionToAdd is hooked.
 * @param callable $functionToAdd The name of the function you wish to be called.
 * @param int $priority [optional] Used to specify the order in which
 *                      the functions associated with a particular
 *                      action are executed (default: 50).
 *                      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.
 * @param ?string $== false to check if it's true!
 *
 * @param string $tag The name of the action hook.
 * @param false|string $function_to_check [optional]
 *
 * @return string|int|bool If $function_to_check is omitted,
 *                         returns boolean for whether the hook has
 *                         anything registered.
 *                         When checking a specific function,
 *                         the priority of that hook is returned,
 *                         or false if the function is not attached.
 *                         When using the $function_to_check
 *                         argument, this function may return a non-boolean
 *                         value that evaluates to false (e.g.) 0,
 *                         so use the === operator for testing the return value.
 */
public static function hasAction(
    string $tag,
    false|string $function_to_check = false
): string|int|bool

/**
 * Removes a function from a specified action hook.
 *
 * @param string $tag The action hook to which the function to be removed is hooked.
 * @param callable $functionToRemove The name of the function which should be removed.
 * @param int $priority [optional] The priority of the function (default: 50).
 *
 * @return bool Whether the function is removed.
 */
public static function removeAction(
    string $tag,
    callable $functionToRemove,
    int $priority = Hook\Constants::PRIORITY_NEUTRAL
): bool

/**
 * Add hook for shortcode tag.
 *
 * There can only be one hook for each shortcode. Which means that if another
 * plugin has a similar shortcode, it will override yours or yours will override
 * theirs depending on which order the plugins are  * // [bartag foo="bar"]
 * function bartag_func($attrs) {
 *  $args = shortcodeAttrs(array(
 *    'foo' => 'no foo',
 *    'baz' => 'default baz',
 *  ), $attrs);
 *
 *  return "foo = {$args['foo']}";
 * }
 * addShortcode('bartag', 'bartag_func');
 *
 * Example with enclosed content:
 *
 * // [baztag]content[/baztag]
 * function baztag_func($attrs, $content='') {
 *  return "content = $content";
 * }
 * addShortcode('baztag', 'baztag_func');
 *
 * @param string $tag Shortcode tag to be searched in post content.
 * @param callable $function Hook to run when shortcode is found.
 *
 * @return bool
 */
public static function addShortcode(string $tag, callable $function): bool

/**
 * Search content for shortcodes and filter shortcodes through their hooks.
 *
 * If there are no shortcode tags defined, then the content will be returned
 * without any filtering. This might cause issues when plugins are disabled but
 * the shortcode will still show up in the post or content.
 *
 * @param string $content Content to search for shortcodes.
 *
 * @return string Content with shortcodes filtered out.
 */
public static function doShortcode(string $content): string

/**
 * Whether the passed content contains the specified shortcode.
 *
 * @param string $content
 * @param string $tag
 *
 * @return bool
 */
public static function hasShortcode(string $content, string $tag): bool

/**
 * Removes hook for shortcode.
 *
 * @param string $tag shortcode tag to remove hook for.
 *
 * @return bool
 */
public static function removeShortcode(string $tag): bool