跳到主要内容
万能墙开放平台
插件开发教程

扩展点清单(钩子与过滤器)

全部内容基于万能墙真实的插件平台(清单 + 钩子 + 接口 + 后台页 + 数据 + 定时任务),示例可直接复制运行。

两种扩展点

| 类型 | 注册函数 | 用途 | 返回值 |

| --- | --- | --- | --- |

| 动作 action | addAction($hook, $cb, $priority = 10) | 「发生了什么」,做副作用(记日志、发通知) | 忽略 |

| 过滤器 filter | addFilter($hook, $cb, $priority = 10) | 「改一下数据」,必须把值返回 | 必须 return |

php
addAction('post_created', function ($postId, $userId) {
    pluginLog('my_plugin', 'info', "新帖 #{$postId} 由用户 #{$userId} 发布");
});

addFilter('plugin_render_footer', function ($html) {
    return $html . '<div>页脚附加内容</div>';   // 忘记 return 就等于把页面清空了
}, 20);

可用动作(action)

| 钩子 | 参数 | 触发时机 |

| --- | --- | --- |

| plugin_loaded | $slug | 插件被加载时 |

| api_before_action | $action | 任意 API 请求分发前 |

| api_action_done | $action | API 处理完成后 |

| api_request | $action, $payload | 请求进入时(含 GET/POST 原始数据) |

| post_created | $postId, $userId | 发帖成功 |

| post_deleted | $postId | 帖子删除(进回收站) |

| post_restored | $postId | 帖子从回收站恢复 |

| post_purged | $postId | 帖子被彻底删除 |

| comment_created | $commentId, $postId | 评论发布成功 |

| user_registered | $userId, $username | 新用户注册 |

| plugin_settings_saved | $slug, $settings | 插件设置保存后 |

| admin_action | $keys, $page, $adminId, $_POST | 后台任意 POST(插件自处理后台表单靠它) |

可用过滤器(filter)

| 钩子 | 参数 | 你能改什么 |

| --- | --- | --- |

| plugin_render_head | $html, $payload | </head> 前注入(CSS / meta) |

| plugin_render_body_top | $html, $payload | <body> 后立刻注入 |

| plugin_render_footer | $html, $payload | </body> 前注入(JS / 悬浮组件) |

| plugin_render_nav_extra | $html, $payload | 导航栏右侧注入入口 |

| api_response | $response, $action | 统一响应体(加字段 / 改写错误) |

| feed_posts | $posts | 信息流帖子数组(加字段 / 过滤) |

| moderation_words | $words | AI 审核的关键词线索 |

| moderation_verdict | $verdict, $content | 审核结论(可覆盖放行/拦截) |

| post_publish_guard | $error, $payload | 发帖前拦截:返回非空字符串即拒绝发布 |

| comment_publish_guard | $error, $payload | 评论前拦截 |

示例:发帖前拦截含「广告」的内容

php
addFilter('post_publish_guard', function ($error, $payload) {
    if ($error !== '') return $error;                 // 尊重其它插件的结论
    $content = (string)($payload['content'] ?? '');
    if (mb_strpos($content, '广告') !== false) {
        return '帖子包含不允许的内容:广告';
    }
    return '';
});

> 想加新钩子?核心没有的扩展点不能凭空生效 —— 需要先在万能墙核心落一个 applyFilters('xxx') / doAction('xxx')。提插件时可以顺带在描述里说明希望增加的扩展点。