WordPress文章防复制代码

通过下面的JS代码,可以有效地防止别人直接复制拷贝你的文章,用frame标签引用你的文章时,会自动跳转到文章正常链接,同时禁止右键菜单。

使用方法一:

打开当前主题头部模

板header.php找到:<?php wp_head(); ?>将下面代码添加到后面:

复制代码
<script>
// 禁止右键www.wwttl.com提供学习
document.oncontextmenu = function() {
 return false
};
// 禁止图片拖放
document.ondragstart = function() {
 return false
};
// 禁止选择文本
document.onselectstart = function() {
 if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password") return false;
 else return true;
};
if (window.sidebar) {
  document.onmousedown = function(e) {
 var obj = e.target;
 if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD") return true;
 else return false;
 }
};
// 禁止frame标签引用
if (parent.frames.length > 0) top.location.replace(document.location);
</script>

使用方法二:

上面的方法查看源代码时有些乱,可以在当前主题目录新建一个名称为copyright.js文件,将下面代码添加进去:

复制代码
// 禁止右键
document.oncontextmenu = function() {
 return false
};
// 禁止图片拖放
document.ondragstart = function() {
 return false
};
// 禁止选择文本
document.onselectstart = function() {
 if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password") return false;
 else return true;
};
if (window.sidebar) {
  document.onmousedown = function(e) {
 var obj = e.target;
 if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD") return true;
 else return false;
 }
};
// 禁止frame标签引用
if (parent.frames.length > 0) top.location.replace(document.location);
一键复制

然后再将下面代码添加到当前主题函数模板functions.php的最后:

复制代码
function copyrightpro_scripts() {
 wp_enqueue_script( 'copyright', get_template_directory_uri() . '/copyright.js', array(),  false );
}
if (! current_user_can('level_10') ) {
add_action( 'wp_enqueue_scripts', 'copyrightpro_scripts' );
}

代码中加了判断,管理员登录状态一下,防复制代码无效。

当然上面的方法,也只是忽悠一下小白,浏览器禁用JavaScript后,将失去效果。

JS代码提取自:CopyRightPro插件

相关推荐
陈随易5 小时前
bm2,Node.js 与 Bun 项目部署新选择
前端·后端·程序员
福兮说6 小时前
canvas 旋转图片的六个坑:四角被裁、Math.ceil 多出 1px 黑边、JPG 角发黑、翻转方向反了
前端·javascript·图像处理·canvas
传人once7 小时前
多图片上传预览功能如何写
前端·css·html·jquery·html5
Eric.468 小时前
Wan‑Animate+MiniMax-H3 本地部署 AI 漫剧流水线:8G 显存动作迁移与动态分镜工程实战
前端·人工智能·comfyui·ai漫剧
搭贝8 小时前
上厕所的时间搭好一套系统:AI自动生成实测
开发语言·人工智能·低代码·ai·php·搭贝
智感子8 小时前
智能称重·数据上云:从仪表读数到可追溯的计量凭证
开发语言·php
库拉镜像AI牛牛9 小时前
短剧内容自动化生产:知漫剧工作室落地教程
大数据·服务器·前端·人工智能·语音识别
律宏阔9 小时前
Flutter 列表图片内存优化:CachedNetworkImage 与 ClipRRect 的取舍
前端·flutter
行者全栈架构师9 小时前
【鸿蒙心迹】相册文搜图工程化落地:HarmonyOS 7.0 端侧 AI 检索全链路实战与 5 个踩坑解法
前端
律宏阔9 小时前
Dart 3 Record:解决 Future.wait 异构返回值的类型问题
前端·flutter