您现在的位置是:首页 > cms教程 > shopxo教程shopxo教程

shopxo插件开发钩子引入静态css/js的方法

阿森2025-07-25shopxo教程已有人查阅

导读如果一些小插件,没有控制器,但是页面显示的内容可能需要css或者js,我们也可以在config.json里面定义 plugins_css 或 plugins_js 钩子

如果一些小插件,没有控制器,但是页面显示的内容可能需要css或者js,我们也可以在config.json里面定义 plugins_css 或 plugins_js 钩子
{
"base":{
"plugins":"test_xxx",
"name":"测试插件",
"logo":"\/static\/upload\/images\/plugins_test_xxx\/2025\/04\/21\/1745219584289850.jpg",
"author":"Devil",
"author_url":"https:\/\/shopxo.net\/",
"version":"1.0.0",
"desc":"测试插件的开发",
"apply_terminal":[
"pc",
"h5"
],
"apply_version":[
"6.5.0"
],
"is_home":false
},
"extend":"",
"hook":{
"plugins_css":[
"app\\plugins\\test_xxx\\Hook"
],
"plugins_js":[
"app\\plugins\\test_xxx\\Hook"
]
}
}
Hook.php里面使用指定自己随便创建的css或js文件,路径位置都没要求,如下:
public/static/plugins/text_xxx/css/index/public/style.css
public/static/plugins/text_xxx/js/index/public/style.js
public/static/plugins/text_xxx/js/index/public/style2.js
public/static/plugins/text_xxx/js/index/public/style3.js
<?php
namespace app\plugins\test_xxx;
// 测试插件 - 钩子入口
class Hook
{
// 应用响应入口
public function handle($params = [])
{
// 钩子名称
if(!empty($params['hook_name']))
{
// 走钩子
$ret = '';
switch($params['hook_name'])
{
case 'plugins_css' :
$ret = 'static/plugins/text_xxx/css/index/public/style.css';
break;
case 'plugins_js' :
// 我们也可以自己通过一些条件限制,哪些页面需要引入在这里做判断即可
// 单个文件引入
$ret = 'static/plugins/text_xxx/js/index/public/style.js';
// 如果要同时引入多个文件也可以使用数组的方式,如:
$ret = [
'static/plugins/text_xxx/js/index/public/style.js',
'static/plugins/text_xxx/js/index/public/style2.js',
'static/plugins/text_xxx/js/index/public/style3.js',
];
break;
}
return $ret;
}
}
}
?>
如果静态的代码太少,您觉得引入文件太麻烦了,也可以直接写css或者js,直接使用头尾钩子 plugins_common_header 和 plugins_common_page_bottom  参考如下:
config.json 文件
{
"base":{
"plugins":"test_xxx",
"name":"测试插件",
"logo":"\/static\/upload\/images\/plugins_test_xxx\/2025\/04\/21\/1745219584289850.jpg",
"author":"Devil",
"author_url":"https:\/\/shopxo.net\/",
"version":"1.0.0",
"desc":"测试插件的开发",
"apply_terminal":[
"pc",
"h5"
],
"apply_version":[
"6.5.0"
],
"is_home":false
},
"extend":"",
"hook":{
"plugins_common_header":[
"app\\plugins\\test_xxx\\Hook"
],
"plugins_common_page_bottom":[
"app\\plugins\\test_xxx\\Hook"
]
}
}
Hook.php 文件
<?php
namespace app\plugins\test_xxx;
// 测试插件 - 钩子入口
class Hook
{
// 应用响应入口
public function handle($params = [])
{
// 钩子名称
if(!empty($params['hook_name']))
{
// 走钩子
$ret = '';
switch($params['hook_name'])
{
// 网站head内 插入css代码
case 'plugins_common_header' :
$ret = '<style type="text/css">
.name {
font-size: 200px;
color: #f00;
}
</style>';
break;
// 网站底部插入js代码
case 'plugins_common_page_bottom' :
$ret = '<script type="text/javascript">
alert("test_xxx js");
</script>';
break;
}
return $ret;
}
}
}
?>
PS:config.json 文件增加或移除钩子后,需要在后台左侧 应用 -> 应用管理里面重启插件

本文标签:

很赞哦! ()

相关源码

  • (自适应)绿色宠物门诊兽医行业网站模板该绿色清爽风格网站模板专为宠物门诊与兽医诊所设计,基于PbootCMS内核开发,采用响应式布局确保手机、PC等多端体验一致,帮助宠物医疗机构高效构建线上服务平台。查看源码
  • pbootcms源码宠物类网站源码下载(自适应多端)为宠物装备商店、宠物食品及用品企业打造的营销型模板,基于PbootCMS内核深度开发。采用响应式设计实现PC与移动端适配,PHP7.0+高性能架构支持MySQL/SQLite双数据库查看源码
  • (自适应)帝国cms7.5模板自媒体文章新闻博客为帝国CMS7.5设计的响应式模板,采用H5技术构建现代化内容展示框架。通过智能断点检测技术实现手机、平板、PC三端适配查看源码
  • (自适应)宽屏大气红色机械设备pbootcms模板源码下载为机械设备制造企业设计的响应式网站模板,采用PbootCMS内核开发。宽屏布局突出设备展示效果,红色工业风格贴合机械行业属性,支持PC端与手机端自动适配查看源码
  • (自适应)蓝色建材亚克力板材装饰材料pbootcms网站模板这款基于PbootCMS开发的响应式模板为建材板材行业优化设计,适用于亚克力板材、建筑装饰材料等企业展示需求。采用HTML5技术实现手机/PC跨终端适配,管理员通过统一后台即可同步查看源码
  • (PC+WAP)地暖热水器烘干机节能设备网站模板下载为地暖热水器及节能设备企业设计的PbootCMS网站模板,集成产品展示、节能方案介绍、技术参数说明等专业模块。采用PC与移动端同步响应架构查看源码
分享笔记 (共有 篇笔记)
验证码:

本栏推荐