freesoftwarefactory / php-hooks
WordPress 过滤器钩子系统的分支,将其封装成类,以移植到任何基于PHP的系统
This package is auto-updated.
Last update: 2024-09-17 21:11:16 UTC
README
PHP Hooks 类是WordPress过滤器钩子系统的分支,将其封装成类,以移植到任何基于PHP的系统
- 这个类主要基于WordPress插件API,大部分(如果不是全部)代码都来自那里。
前往 http://bainternet.github.com/PHP-Hooks/ 获取更多信息
如何使用?
简单,将类文件包含在您的应用程序引导文件中(设置/加载/配置或您称之为什么),然后使用全局 $hooks 开始钩接您的过滤器和动作钩子。例如:
include_once('php-hooks.php'); global $hooks; $hooks->add_action('header_action','echo_this_in_header'); function echo_this_in_header(){ echo 'this came from a hooked function'; }
然后剩下的就是当您在应用程序的任何地方需要时调用钩接的函数,例如:
echo '<div id="extra_header">'; global $hooks; $hooks->do_action('header_action'); echo '</div>';
您的输出将是
<div id="extra_header">this came from a hooked function</div>
命名空间/Composer
此条目应存在于您的 vendor/composer/autoload_psr4.php 中,无论是通过Composer自动添加(正确的方式),还是手工添加。
'freesoftwarefactory\\hooks\\' => array($vendorDir . '/freesoftwarefactory/php-hooks'),
用法
use freesoftwarefactory\hooks; $myhook = new Hooks; ...
方法
动作
add_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 执行在特定动作钩子上注册的函数。
- @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 从指定的动作钩子中移除函数。
- @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 检查是否有任何动作已注册到钩子。
- @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 获取动作被触发的次数。
- @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
过滤器
add_filter 将函数或方法钩接到特定的过滤器动作。
- @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 从指定的过滤器钩子中移除函数。
- @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 检查是否有任何过滤器已注册到钩子。
- @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 调用添加到过滤器钩子的函数。
- @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.
还有更多方法,但这些都是您将主要使用的方法:
下载
您也可以通过运行以下命令使用Git克隆项目:
$ git clone git://github.com/bainternet/PHP-Hooks.git
许可证
由于这个类是从WordPress插件API派生出来的,因此许可证也是GPL https://gnu.ac.cn/licenses/gpl.html