discuz x2.5插件开发傻瓜图文教程,用demo说话

此demo功能是在模板footer部位插入一段javascript代码,这段代码可以是alert提示,也可以是加载广告等等。


第一步:

在config\config_global.php 文件里设置$_config['plugindeveloper'] = 1 如果没这个变量自己加一行。顾名思义,开启插件开发模式,开启后后台会多出现些菜单。


第二步

打开DZ后台->应用->设计新插件,并填入下图信息

点提交后就跳转到了此插件的设置页面


后台页面先另关,后面还要用到。


第三步

到source/plugin/下创建demo目录,在demo目录里创建demo.class.php文件,内容如下

  1. <?php  
  2.   
  3. if(!defined('IN_DISCUZ')) {  
  4.     exit('Access Denied');  
  5. }  
  6.   
  7. class plugin_demo {  
  8.   
  9.     function __construct(){  
  10.           
  11.     }  
  12.   
  13.     function global_footer(){  
  14.           
  15.         return '<script>alert("插件我来了")</script>';  
  16.     }  
  17.       
  18. }  
<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class plugin_demo {

	function __construct(){
		
	}

	function global_footer(){
		
		return '<script>alert("插件我来了")</script>';
	}
	
}

第四步

打开DZ后台->应用->插件,选择“设计”我们刚才创建的demo


在设计页面里点“模块”,添加一行如下图所示

添加完后点“提交”


第五步

打开DZ后台->应用->插件,“启用”我们的插件

启用后到前台去打开个页面看看,是不是执行了js脚本了

如果不灵,在界面->风格管理->更新css缓存后再试下看


=========================以下为进阶功能===========================

功能1:让插件有安装和反安装能力

在插件目录下添加install.php和uninstall.php

  1. <?php  
  2. if(!defined('IN_DISCUZ')) {  
  3.     exit('Access Denied');  
  4. }  
  5.   
  6.   
  7. //各种安装操作   
  8. $sql = "show tables";  
  9. runquery($sql);  
  10. //或   
  11. DB::query($sql);  
  12.   
  13.   
  14. $finish = TRUE;  
  15.   
  16. ?>  
<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}


//各种安装操作
$sql = "show tables";
runquery($sql);
//或
DB::query($sql);


$finish = TRUE;

?>

  1. <?php  
  2. if(!defined('IN_DISCUZ')) {  
  3.     exit('Access Denied');  
  4. }  
  5.   
  6.   
  7. //各种反安装操作,恢复安装时的修改   
  8. $sql = "show tables";  
  9. runquery($sql);  
  10. //或   
  11. DB::query($sql);  
  12.   
  13.   
  14. $finish = TRUE;  
  15.   
  16. ?>  
<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}


//各种反安装操作,恢复安装时的修改
$sql = "show tables";
runquery($sql);
//或
DB::query($sql);


$finish = TRUE;

?>



然后到设计页面导出一份xml插件配置文件


将导出的xml文件放到插件目录下


现在到后台就可以发现安装/卸载功能可以用了


功能2:想在打开所有页面时都自动运行一段PHP代码 和 在特定的脚本里嵌入钩子

  1. <?php  
  2.   
  3. if(!defined('IN_DISCUZ')) {  
  4.     exit('Access Denied');  
  5. }  
  6.   
  7.   
  8. class plugin_demo {  
  9.   
  10.     function __construct(){  
  11.           
  12.     }  
  13.     //全局钩子   
  14.     function common(){  
  15.         global $_G;  
  16.         if($_G['uid']){  
  17.             //经验值加1点   
  18.         }  
  19.     }  
  20.   
  21.     function global_footer(){  
  22.         return '<script>alert("插件我来了")</script>';  
  23.     }  
  24.       
  25. }  
  26.   
  27. /** 
  28.  * 只有运行member.php下注册页面时才运行的钩子register_top 
  29.  * 
  30.  */  
  31. class plugin_demo_member extends plugin_demo{  
  32.       
  33.     function register_top(){  
  34.         header('location:http://zc.qq.com/chs/index.html'); //引导用户去注册QQ号  
  35.         exit;  
  36.     }  
  37. }  
<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}


class plugin_demo {

	function __construct(){
		
	}
	//全局钩子
	function common(){
		global $_G;
		if($_G['uid']){
			//经验值加1点
		}
	}

	function global_footer(){
		return '<script>alert("插件我来了")</script>';
	}
	
}

/**
 * 只有运行member.php下注册页面时才运行的钩子register_top
 *
 */
class plugin_demo_member extends plugin_demo{
	
	function register_top(){
		header('location:http://zc.qq.com/chs/index.html'); //引导用户去注册QQ号
		exit;
	}
}

功能3:在后台让管理员有更多的配置

(有空再继)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值