tombroucke/wp-fluent-hooks 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tombroucke/wp-fluent-hooks

Composer 安装命令:

composer require tombroucke/wp-fluent-hooks

包简介

A small utility package that provides a modern, fluent interface for adding WordPress action and filter hooks. Chain methods to build your hooks in a more expressive and readable way.

README 文档

README

A fluent interface for registering and deregistering WordPress filters and actions. Instead of repeating the hook name across multiple add_filter() and remove_filter() calls, you chain everything together. This makes your hook logic easier to read and maintain.

// Before
remove_filter('woocommerce_before_shop_loop', 'woocommerce_result_count', 20);
remove_filter('woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30);

// After
Action::hook('woocommerce_before_shop_loop')
    ->deregister('woocommerce_result_count', 20)
    ->deregister('woocommerce_catalog_ordering', 30);

Examples

Filters

Filter::hook('the_title')
    ->register(fn ($title) => strtoupper($title));

With priority and argument count:

Filter::hook('save_post')
    ->register(function ($postId, $post, $update) {
        // Do something
    }, priority: 11, args: 3);

Actions

Action and Filter share the same API and can be used interchangeably.

Action::hook('init')
    ->register(function () {
        // Do something
    });

Deregistering

Deregister external hooks by passing the callback name and priority inline:

Action::hook('woocommerce_before_shop_loop')
    ->deregister('woocommerce_result_count', 20)
    ->deregister('woocommerce_catalog_ordering', 30);

Chain deregistering and registering on the same hook. Use chainable methods priority() and args() to set values for the rest of the chain:

Action::hook('woocommerce_before_main_content')
    ->priority(20)
    ->deregister('woocommerce_breadcrumb')
    ->register(function () {
        yoast_breadcrumb('<p class="small breadcrumb">', '</p>');
    });

Conditional registration

Use when() to conditionally run a registered callback. The condition receives the same arguments as the filter/action and is evaluated at the time the hook fires

Filter::hook('the_title')
    ->when(fn ($title) => strlen($title) > 10)
    ->register(fn ($title) => strtoupper($title));

Use always() to remove a previously set condition:

Filter::hook('the_title')
    ->when(fn ($title) => strlen($title) > 10)
    ->register(fn ($title) => strtoupper($title))
    ->always()
    ->register(fn ($title) => strtolower($title));

when() currently cannot be combined with deregister(). You will need to find a workaround for now.

Deferring to another hook

Use at() to defer register() or deregister() until another hook fires. This is useful when the context you need (e.g. the current post, the current page) is not yet available at the time your code runs.

When a plugin or theme nests the actions and filters

add_action('template_redirect', function () {
    add_filter('the_title', 'strtoupper');
}, 100);

You can

Filter::hook('the_title')
    ->at('template_redirect', 101)
    ->deregister('strtoupper');

Aliases

Only hooks registered with an alias are tracked internally. Without an alias, the hook is registered with WordPress but cannot be deregistered via this library. Assign an alias to reference a hook registered via this library:

Action::hook('body_class')
    ->alias('my_custom_body_class')
    ->register(fn ($classes) => array_merge($classes, ['custom-class']));

// Later:
Action::hook('body_class')
    ->deregister('my_custom_body_class');

tombroucke/wp-fluent-hooks 适用场景与选型建议

tombroucke/wp-fluent-hooks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 11, 最近一次更新时间为 2026 年 04 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 tombroucke/wp-fluent-hooks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tombroucke/wp-fluent-hooks 我们能提供哪些服务?
定制开发 / 二次开发

基于 tombroucke/wp-fluent-hooks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 16
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 37
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2026-04-30