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插件

相关推荐
耶啵奶膘2 小时前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
视频砖家3 小时前
移动端Html5播放器按钮变小的问题解决方法
前端·javascript·viewport功能
lyj1689973 小时前
vue-i18n+vscode+vue 多语言使用
前端·vue.js·vscode
小白变怪兽5 小时前
一、react18+项目初始化(vite)
前端·react.js
ai小鬼头5 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
墨菲安全5 小时前
NPM组件 betsson 等窃取主机敏感信息
前端·npm·node.js·软件供应链安全·主机信息窃取·npm组件投毒
GISer_Jing5 小时前
Monorepo+Pnpm+Turborepo
前端·javascript·ecmascript
天涯学馆5 小时前
前端开发也能用 WebAssembly?这些场景超实用!
前端·javascript·面试
我在北京coding7 小时前
TypeError: Cannot read properties of undefined (reading ‘queryComponents‘)
前端·javascript·vue.js
前端开发与ui设计的老司机7 小时前
UI前端与数字孪生结合实践探索:智慧物流的货物追踪与配送优化
前端·ui