分类编辑页表单输出扩展
通过 Filter_Plugin_Category_Edit_Response 接口,可以在 Z-BlogPHP 后台分类编辑页表单字段之后、提交按钮之前追加自定义表单内容,适用于为分类增加自定义字段(如分类 banner 图片、SEO 标题等)的场景。该接口在文章分类和独立页面分类的编辑页(category_edit.php)中均生效。
接口信息
| 接口 | 参数 | 说明 |
|---|---|---|
Filter_Plugin_Category_Edit_Response | 无 | 分类编辑页表单字段之后输出内容 |
完整案例
下例在分类编辑页增加一个「SEO 标题」输入框:
php
<?php
RegisterPlugin("demoAPP", "ActivePlugin_demoAPP");
function ActivePlugin_demoAPP()
{
Add_Filter_Plugin('Filter_Plugin_Category_Edit_Response', 'demoAPP_Category_Edit_Response');
}
// 显示:在分类编辑页输出输入框
function demoAPP_Category_Edit_Response()
{
global $cate;
$seoTitle = isset($cate->Metas->demoapp_seotitle) ? $cate->Metas->demoapp_seotitle : '';
echo '<p><span class="title">SEO 标题:</span><br />';
echo '<input class="edit" size="40" name="meta_demoapp_seotitle" type="text" value="' . htmlspecialchars($seoTitle) . '" /></p>';
}数据保存
插件不需要自己写保存代码。Z-BlogPHP 在文章、独立页面、分类、标签、用户、模块的保存流程中都会执行 FilterMeta(),自动把表单中所有 meta_ 前缀的字段写入对应对象的 Metas:字段 meta_demoapp_seotitle 保存后即为 $cate->Metas->demoapp_seotitle,前台模板中同样可通过该方式读取。
需要处理非 meta_ 前缀字段或做额外逻辑时,才需要挂载 Filter_Plugin_PostCategory_Succeed 等保存接口。
注意事项
- 接口没有参数,回调函数通过
echo输出内容,输出位置在「加入导航栏」字段之后、提交按钮之前,被包裹在<div id='response' class='editmod2'>内;系统表单字段使用<p><span class="title">...</span></p>段落结构,插件自行输出时建议与之保持统一风格; meta_前缀字段的值为空字符串时该 Meta 键会被自动删除,因此读取时应先用isset()判断键是否存在;- 回调中可通过
global $cate访问当前正在编辑的分类对象,新建分类时ID为 0; - 分类编辑页支持通过
type参数区分文章分类和独立页面分类,插件字段对两种类型均生效;如需区分处理,可读取$cate->Type(0 为文章分类,1 为独立页面分类)。