PHP Classes

PHP WordPress Hooks and Filters: Handle and triggers actions like WordPress

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 64%Total: 187 All time: 8,628 This week: 660Up
Version License PHP version Categories
php-hooks 0.2.14GNU General Publi...5.3PHP 5, Language
Collaborate with this project 

Authors

Ohad Raz
Lars Moelleken


Contributor

php-hooks - github.com

Description

This class can handle and triggers actions like WordPress.

It can register handlers for actions that can be triggered in a way similar to WordPress. It can:

- Register a handler for an action
- Trigger an action to be handled by all handlers
- Remove a given action handler
- Check if an action handler was registered
- Get the number of times an action was triggered
- Adds an action filter handler
- Remove a given action filter handler
- Check if are there any registered action filters
- Apply all the filters for a given action

Innovation Award
PHP Programming Innovation award nominee
October 2016
Number 4
WordPress is one of the most popular PHP applications. Part of its popularity came from the fact that it can be extended with thousands of plugins that customize its behavior and functionality.

Those plugins can extend WordPress functionality by the means of callback functions that execute custom code when certain actions happen, as well it can filter data that is generated to present the site Web pages.

This package implements a plugin system like the one in WordPress with action and filter hooks, except that those hooks are registered and manipulated using class objects.

Manuel Lemos
Picture of Lars Moelleken
  Performance   Level  
Name: Lars Moelleken <contact>
Classes: 25 packages by
Country: Germany Germany
Age: 36
All time rank: 62140 in Germany Germany
Week rank: 22 Up2 in Germany Germany Up
Innovation award
Innovation award
Nominee: 11x

Winner: 1x

Documentation

Build Status Coverage Status Scrutinizer Code Quality Codacy Badge SensioLabsInsight Latest Stable Version Total Downloads Latest Unstable Version PHP 7 ready License

PHP-Hooks

The PHP Hooks Class is a fork of the WordPress filters hook system rolled in to a class to be ported into any php based system * This class is heavily based on the WordPress plugin API and most (if not all) of the code comes from there.

How to install?

composer require voku/php-hooks

How to use?

We start with a simple example ...

<?php

$hooks = Hooks::getInstance();

$hooks->add_action('header_action','echo_this_in_header');

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

then all that is left for you is to call the hooked function when you want anywhere in your application, EX:

<?php

$hooks = Hooks::getInstance();

echo '<div id="extra_header">';
$hooks->do_action('header_action');
echo '</div>';

and you output will be: <div id="extra_header">this came from a hooked function</div>

PS: you can also use method from a class for a hook e.g.: $hooks->add_action('header_action', array($this, 'echo_this_in_header_via_method');

Methods

ACTIONS:

add_action Hooks a function on to a specific action.

 - @access public
 - @since 0.1
 - @param string $tag The name of the action to which the $function_to_add is hooked.
 - @param callback $function_to_add 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: 10). 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 int $accepted_args optional. The number of arguments the function accept (default 1).

do_action Execute functions hooked on a specific action hook.

 - @access public
 - @since 0.1
 - @param string $tag The name of the action to be executed.
 - @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
 - @return null Will return null if $tag does not exist

remove_action Removes a function from a specified action hook.

 - @access public
 - @since 0.1
 - @param string $tag The action hook to which the function to be removed is hooked.
 - @param callback $function_to_remove The name of the function which should be removed.
 - @param int $priority optional The priority of the function (default: 10).
 - @return boolean Whether the function is removed.

has_action Check if any action has been registered for a hook.

 -  @access public
 -  @since 0.1
 -  @param string $tag The name of the action hook.
 -  @param callback $function_to_check optional.
 -  @return mixed 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.

did_action Retrieve the number of times an action is fired.

 - @access public
 - @since 0.1
 - @param string $tag The name of the action hook.
 - @return int The number of times action hook <tt>$tag</tt> is fired

FILTERS:

add_filter Hooks a function or method to a specific filter action.

 - @access public
 - @since 0.1
 - @param string $tag The name of the filter to hook the $function_to_add to.
 - @param callback $function_to_add 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: 10). 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 int $accepted_args optional. The number of arguments the function accept (default 1).
 - @return boolean true

remove_filter Removes a function from a specified filter hook.

 - @access public
 - @since 0.1
 - @param string $tag The filter hook to which the function to be removed is hooked.
 - @param callback $function_to_remove The name of the function which should be removed.
 - @param int $priority optional. The priority of the function (default: 10).
 - @param int $accepted_args optional. The number of arguments the function accepts (default: 1).
 - @return boolean Whether the function existed before it was removed.

has_filter Check if any filter has been registered for a hook.

 - @access public
 - @since 0.1
 - @param string $tag The name of the filter hook.
 - @param callback $function_to_check optional.
 - @return mixed 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.

apply_filters Call the functions added to a filter hook.

 - @access public
 - @since 0.1
 - @param string $tag The name of the filter hook.
 - @param mixed $value The value on which the filters hooked to <tt>$tag</tt> are applied on.
 - @param mixed $var,... Additional variables passed to the functions hooked to <tt>$tag</tt>.
 - @return mixed The filtered value after all hooked functions are applied to it.

License

Since this class is derived from the WordPress Plugin API so are the license and they are GPL http://www.gnu.org/licenses/gpl.html

[1]: https://github.com/bainternet/PHP-Hooks/zipball/master [2]: https://github.com/bainternet/PHP-Hooks/tarball/master [3]: http://bainternet.github.com/PHP-Hooks/


  Files folder image Files (17)  
File Role Description
Files folder imagesrc (1 directory)
Files folder imagetests (8 files)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file license.txt Doc. Documentation
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (17)  /  src  
File Role Description
Files folder imagevoku (1 directory)

  Files folder image Files (17)  /  src  /  voku  
File Role Description
Files folder imagehelper (1 file)

  Files folder image Files (17)  /  src  /  voku  /  helper  
File Role Description
  Plain text file Hooks.php Class Class source

  Files folder image Files (17)  /  tests  
File Role Description
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file HooksActionTest.php Class Class source
  Plain text file HooksFilterTest.php Class Class source
  Plain text file HooksFooBar.php Class Class source
  Plain text file HooksShortcodeStrictTest.php Class Class source
  Plain text file HooksShortcodeTest.php Class Class source
  Plain text file HooksStrictTest.php Class Class source
  Plain text file HooksTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:187
This week:0
All time:8,628
This week:660Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:95%StarStarStarStarStar
Documentation:87%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:64%StarStarStarStar
Rank:734