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

前台页面与短代码

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

注册前台页面

php
registerFrontView('my_plugin', 'index', function ($ctx) {
    echo '<div class="container" style="padding:24px">';
    echo '<h1>我的插件页</h1><p>这里是插件自己的页面。</p>';
    echo '</div>';
});

访问地址:plugin.php?p=my_plugin&view=index

(想更好看的 URL,可以在插件页里说明,或让站长加一条 nginx 伪静态指向它。)

渲染注入(不用改核心模板)

php
addFilter('plugin_render_head', function ($html) {
    return $html . '<link rel="stylesheet" href="plugin_asset.php?p=my_plugin&f=style.css">';
});
addFilter('plugin_render_body_top', function ($html) { return $html . '<div class="tip">维护中</div>'; });
addFilter('plugin_render_footer', function ($html) { return $html . '<script src="plugin_asset.php?p=my_plugin&f=app.js"></script>'; });
addFilter('plugin_render_nav_extra', function ($html) { return $html . '<a class="nav-item" href="plugin.php?p=my_plugin">我的入口</a>'; });

> 注入的 HTML 会直接进入页面,所有用户输入都要 htmlspecialchars(),否则就是 XSS 漏洞。

短代码

php
addShortcode('hello', function ($attrs, $inner) {
    $name = htmlspecialchars((string)($attrs['name'] ?? '同学'), ENT_QUOTES, 'UTF-8');
    return '<span style="color:#4f46e5">你好,' . $name . '!</span>';
});

帖子 / 公告里写 [hello name="小明"] 即可渲染;成对写法 [hello]内容[/hello] 的 $inner 是中间内容。

前台页里的登录态

前台页面是「页面壳 + API 拉数据」的结构。要拿当前登录态,用核心的 API 客户端助手:

php
$ctx = function_exists('api_client_page_ctx') ? api_client_page_ctx() : [];
$user = is_array($ctx['user'] ?? null) ? $ctx['user'] : null;   // 未登录为 null

自己写的读接口建议声明 'auth' => 'none' 并在返回里按 $ctx['user_id'] 决定给什么字段。