目录
- 第一部分:基础准备
- 什么是DedeCMS插件?
- 开发环境准备
- 第二部分:插件入门 - "Hello World"
- 第1步:创建插件目录和核心文件
- 第2步:编写安装和卸载脚本
- 第3步:在后台管理界面中显示插件
- 第4步:安装并测试插件
- 第三部分:进阶开发 - 创建数据表和模块
- 第1步:创建数据表
- 第2步:修改
install.php以创建数据表 - 第3步:创建数据模型和后台管理模块
- 第4步:在前台调用插件数据
- 第四部分:核心概念与文件结构详解
- 文件结构解析
- 关键函数和变量
- 安全与规范
- 第五部分:发布与分享你的插件
- 打包插件
- 提交官方市场
第一部分:基础准备
什么是DedeCMS插件?
DedeCMS插件是一种功能模块化的程序,它允许你在不修改DedeCMS核心文件的情况下,为网站增加新的功能,插件通常包含后台管理、前台展示、数据处理等部分,遵循DedeCMS的规范,可以方便地安装、启用、禁用和卸载。

开发环境准备
- 本地服务器环境:推荐使用
phpStudy、XAMPP或WAMP等集成环境,确保你的电脑上运行着一个PHP + MySQL环境。 - DedeCMS程序:从DedeCMS官网下载一个稳定版本的程序(如DedeCMS V5.7 SP2),并安装在你的本地服务器上。
- 代码编辑器:如 VS Code、Sublime Text、Notepad++ 等,用于编写和编辑代码。
- FTP/SFTP工具:用于将文件上传到服务器(本地开发时也可以直接操作文件目录)。
第二部分:插件入门 - "Hello World"
我们的目标是开发一个插件,在后台“插件管理”菜单中增加一个入口,点击后页面显示 "Hello, Dede Plugin World!"。
第1步:创建插件目录和核心文件
所有插件都存放在 /dede/ 目录下,我们需要为我们的插件创建一个专属目录。
-
在
/dede/目录下创建一个新文件夹,命名为helloworld。 -
在
/dede/helloworld/目录下,创建以下核心文件:
(图片来源网络,侵删)helloworld.php(插件入口文件)helper.php(辅助函数文件,可选,但推荐)install.php(安装脚本)uninstall.php(卸载脚本)helloworld_addon.sql(数据库结构文件,可选)
helloworld.php (插件入口文件)
这个文件是插件的核心,它定义了插件的基本信息和要执行的操作。
<?php
/**
* DedeCMS Hello World 插件
* @version: 1.0
* @author: Your Name
* @email: your@email.com
*/
if(!defined('DEDEINC')) exit('dedecms');
// 获取系统配置
$aid = isset($aid) && is_numeric($aid) ? $aid : 0;
// 根据不同的操作执行不同的逻辑
if($aid == 1) {
// 如果aid为1,则显示内容
echo "Hello, Dede Plugin World! This is a simple plugin demo.";
} else {
// 默认显示,可以是一个表单或说明
echo "<h2>Hello World Plugin</h2>";
echo "<p>This is the main page of the Hello World plugin.</p>";
echo "<a href='helloworld.php?aid=1'>Click here to see the message.</a>";
}
?>
helper.php (辅助函数文件)
这个文件可以用来存放一些可复用的函数,保持主文件的整洁。

<?php
/**
* Hello World 插件辅助函数
*/
if(!defined('DEDEINC')) exit('dedecms');
function helloworld_get_greeting() {
return "Hello from the helper function!";
}
?>
第2步:编写安装和卸载脚本
这两个脚本在插件安装和卸载时由DedeCMS系统自动调用。
install.php (安装脚本)
<?php
/**
* Hello World 插件安装脚本
*/
if(!defined('DEDEINC')) exit('dedecms');
// 插件信息
$plugin_name = 'Hello World';
$plugin_version = '1.0';
$plugin_author = 'Your Name';
$plugin_weburl = 'http://www.yourwebsite.com';
// 插入到数据库的`dede_plugins`表中
$sql = "INSERT INTO `#@__plugins` (pname, pdescription, pversion, pauthor, pwebsite, `config`)
VALUES ('$plugin_name', 'A simple Hello World plugin.', '$plugin_version', '$plugin_author', '$plugin_weburl', '')";
$dsql->ExecuteNoneQuery($sql);
// 提示信息
ShowMsg("插件安装成功!", "plugin.php?m=list");
exit();
?>
uninstall.php (卸载脚本)
<?php
/**
* Hello World 插件卸载脚本
*/
if(!defined('DEDEINC')) exit('dedecms');
// 从数据库中删除插件记录
$sql = "DELETE FROM `#@__plugins` WHERE pname='Hello World'";
$dsql->ExecuteNoneQuery($sql);
// 提示信息
ShowMsg("插件卸载成功!", "plugin.php?m=list");
exit();
?>
第3步:在后台管理界面中显示插件
要让插件出现在后台的“插件管理”列表中,我们需要修改 config.cache.inc.php 文件(DedeCMS V5.7及以后版本推荐使用 inc/inc_config.php 或直接在后台操作)。
但更简单的方式是,在后台手动安装,我们的 install.php 脚本会自动将插件信息写入数据库,系统会自动识别。
第4步:安装并测试插件
- 将
/dede/helloworld/整个文件夹通过FTP上传到你的DedeCMS网站的/dede/目录下。 - 登录DedeCMS后台,进入 “系统” -> “插件管理” -> “插件列表”。
- 你应该能看到名为 "Hello World" 的插件,点击 “安装”。
- 安装成功后,点击插件名称或 “管理” 按钮,你将看到
helloworld.php中定义的输出内容。
第三部分:进阶开发 - 创建数据表和模块
我们扩展插件,让它能够存储和显示一些自定义内容。
第1步:创建数据表
我们需要一个数据表来存储插件的内容,在MySQL中创建一个表,dede_helloworld_data。
CREATE TABLE `dede_helloworld_data` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, varchar(255) NOT NULL DEFAULT '', `content` text NOT NULL, `addtime` int(10) unsigned NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
第2步:修改 install.php 以创建数据表
修改 install.php,在安装插件时同时创建数据表。
// ... (之前的代码) ...
// 创建数据表
$tablesql = "CREATE TABLE IF NOT EXISTS `#@__helloworld_data` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT, varchar(255) NOT NULL DEFAULT '',
`content` text NOT NULL,
`addtime` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
$dsql->ExecuteNoneQuery($tablesql);
// 插入到数据库的`dede_plugins`表中
$sql = "INSERT INTO `#@__plugins` (pname, pdescription, pversion, pauthor, pwebsite, `config`)
VALUES ('$plugin_name', 'A simple Hello World plugin with data table.', '$plugin_version', '$plugin_author', '$plugin_weburl', '')";
$dsql->ExecuteNoneQuery($sql);
// 提示信息
ShowMsg("插件安装成功!", "plugin.php?m=list");
exit();
第3步:创建数据模型和后台管理模块
我们需要一个管理界面来添加、编辑和删除数据,这通常需要一个模型文件和一个模板文件。
-
创建模型文件
/dede/helloworld/helloworld_model.php<?php /** * Hello World 插件数据模型 */ if(!defined('DEDEINC')) exit('dedecms'); class helloworld_model { var $dsql; function __construct() { global $dsql; $this->dsql = $dsql; } // 获取所有数据 function getAllData() { $sql = "SELECT * FROM `#@__helloworld_data` ORDER BY id DESC"; $this->dsql->SetQuery($sql); $this->dsql->Execute(); $data = array(); while($row = $this->dsql->GetArray()) { $data[] = $row; } return $data; } // 添加数据 function addData($title, $content) { $title = addslashes($title); $content = addslashes($content); $addtime = time(); $sql = "INSERT INTO `#@__helloworld_data` (title, content, addtime) VALUES ('$title', '$content', $addtime)"; return $this->dsql->ExecuteNoneQuery($sql); } } ?> -
修改入口文件
/dede/helloworld/helloworld.php,增加管理功能。<?php /** * DedeCMS Hello World 插件 */ if(!defined('DEDEINC')) exit('dedecms'); // 引入模型 require_once(DEDEINC . '/plugin/helloworld/helloworld_model.php'); $model = new helloworld_model(); // 获取操作参数 $action = isset($action) ? trim($action) : ''; // 根据不同操作执行逻辑 if($action == 'add') { // 处理添加表单提交 if(empty($title) || empty($content)) { ShowMsg("标题和内容不能为空!", '-1'); exit(); } if($model->addData($title, $content)) { ShowMsg("添加成功!", "helloworld.php"); } else { ShowMsg("添加失败!", '-1'); } exit(); } else { // 默认显示管理界面 $datalist = $model->getAllData(); include DEDEROOT . '/dede/templets/helloworld_admin.htm'; // 引入模板 } ?> -
创建模板文件
/dede/templets/helloworld_admin.htm<!DOCTYPE html> <html> <head> <meta charset="utf-8">Hello World Plugin Admin</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } .form-group { margin-bottom: 15px; } .form-group label { display: block; margin-bottom: 5px; } .form-group input, .form-group textarea { width: 100%; padding: 8px; } </style> </head> <body> <h2>Hello World Plugin Management</h2> <div class="form-group"> <form action="helloworld.php" method="post"> <input type="hidden" name="action" value="add"> <label for="title">Title:</label> <input type="text" name="title" id="title" required> <label for="content">Content:</label> <textarea name="content" id="content" rows="5" required></textarea> <button type="submit">Add Data</button> </form> </div> <table> <thead> <tr> <th>ID</th> <th>Title</th> <th>Content</th> <th>Add Time</th> </tr> </thead> <tbody> {dede:datalist} <tr> <td>[field:id/]</td> <td>[field:title/]</td> <td>[field:description function="cn_substr(@me, 50)"/]...</td> <td>[field:adtime function="MyDate('Y-m-d H:i:s',@me)"/]</td> </tr> {/dede:datalist} </tbody> </table> </body> </html>注意:这里的模板标签
{dede:datalist}是DedeCMS的底层模板标签,但在这个纯PHP文件中,我们通常直接使用PHP循环来输出数据,上面的模板更像是一个静态HTML示例,在实际PHP代码中,你应该这样做:// 在 helloworld.php 的 else 分支中 $datalist = $model->getAllData(); ?> <!DOCTYPE html> <html> <head><title>Admin</title></head> <body> <h2>Add New Item</h2> <form action="helloworld.php" method="post"> <input type="hidden" name="action" value="add"> Title: <input type="text" name="title"><br> Content: <textarea name="content"></textarea><br> <input type="submit" value="Add"> </form> <hr> <h2>Data List</h2> <table> <tr><th>ID</th><th>Title</th><th>Content</th><th>AddTime</th></tr> <?php foreach($datalist as $item): ?> <tr> <td><?php echo $item['id']; ?></td> <td><?php echo htmlspecialchars($item['title']); ?></td> <td><?php echo htmlspecialchars($item['content']); ?></td> <td><?php echo date('Y-m-d H:i:s', $item['addtime']); ?></td> </tr> <?php endforeach; ?> </table> </body> </html> <?php
第4步:在前台调用插件数据
要在前台页面显示插件数据,你需要创建一个自定义标签。
-
创建标签文件
/include/taglib/helloworld.lib.php<?php /** * Hello World 自定义标签 * 用法: {dede:helloworld row='10'}{/dede:helloworld} */ if(!defined('DEDEINC')) exit('dedecms'); function lib_helloworld(&$ctag, &$refObj) { global $dsql; // 获取标签属性 $attlist = "row|10"; FillAttsDefault($ctag->CAttribute->Items, $attlist); $innertext = trim($ctag->GetInnerText()); $revalue = ''; // 从数据库查询数据 $row = $ctag->CAttribute['row']; $sql = "SELECT * FROM `#@__helloworld_data` ORDER BY id DESC LIMIT 0, $row"; $dsql->SetQuery($sql); $dsql->Execute(); // 循环输出 while($row = $dsql->GetArray()) { $tmp = str_replace('~title~', $row['title'], $innertext); $tmp = str_replace('~content~', $row['content'], $tmp); $revalue .= $tmp; } return $revalue; } ?> -
在前台模板中使用标签
在你的任意DedeCMS前台模板文件(如
index.htm)中,你可以这样使用:<h2>Latest from Hello World Plugin</h2> {dede:helloworld row='5'} <div class="helloworld-item"> <h3>~title~</h3> <p>~content~</p> </div> {/dede:helloworld}
第四部分:核心概念与文件结构详解
文件结构解析
/dede/
└── helloworld/ # 插件根目录
├── helloworld.php # 插件入口文件,处理逻辑和显示
├── helper.php # 辅助函数库
├── install.php # 安装脚本
├── uninstall.php # 卸载脚本
├── helloworld_model.php # 数据模型(可选,推荐)
├── helloworld_addon.sql # 数据库结构文件(可选)
└── images/ # 插件图片目录
└── icon.png
/dede/templets/
└── helloworld_admin.htm # 后台管理模板(可选)
/include/taglib/
└── helloworld.lib.php # 自定义标签库文件(用于前台调用)
关键函数和变量
$dsql: 全局数据库连接对象,用于执行所有SQL查询。$dsql->ExecuteNoneQuery($sql)用于执行增删改,$dsql->SetQuery($sql)+$dsql->Execute()+$dsql->GetArray()用于查询。ShowMsg($msg, $gourl, $onlymsg=0, $limittime=1000): 显示一个操作提示消息,并自动跳转。DEDEINC: 定义了include目录的路径,常用于引入公共文件。defined('DEDEINC'): 每个插件文件开头都必须有这行,用于防止被直接访问。
安全与规范
- SQL注入: 始终对用户输入进行转义,使用
addslashes()或$dsql->GetOne()等安全方法,如果使用$dsql->SetSqlString(),参数会被自动转义。 - XSS攻击: 在输出到HTML前,使用
htmlspecialchars()函数对内容进行转义。 - 文件路径: 尽量使用
DEDEDATA、DEDEINC等预定义常量来构建路径,避免硬编码。
第五部分:发布与分享你的插件
当你完成插件开发并测试无误后,可以将其打包分享给其他人。
打包插件
- 创建一个新的文件夹,以插件名+版本号命名,
helloworld_v1.0。 - 将你的插件目录
/dede/helloworld/中的所有文件和文件夹复制到helloworld_v1.0文件夹中。 - 编写一个简单的
readme.txt文件,说明插件的名称、版本、功能、安装方法、作者信息等。 - 将
helloworld_v1.0文件夹压缩成一个.zip或.rar文件。
提交官方市场
如果你想让你的插件被更多人使用,可以考虑提交到DedeCMS官方插件市场。
- 访问DedeCMS官方网站,找到“插件市场”或开发者中心。
- 按照网站指引注册开发者账号。
- 提交你的插件包和相关说明文档,等待官方审核。
这份教程涵盖了DedeCMS插件开发的主要方面,从最简单的“Hello World”到包含数据模型和自定义标签的完整插件,希望能为你打下坚实的基础,祝你开发顺利!
