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

相关推荐
粥里有勺糖6 分钟前
用Trae做了个公众号小工具
前端·ai编程·trae
棉花糖超人1 小时前
【从0-1的HTML】第2篇:HTML标签
前端·html
exploration-earth1 小时前
本地优先的状态管理与工具选型策略
开发语言·前端·javascript
OpenTiny社区1 小时前
开源之夏报名倒计时3天!还有9个前端任务有余位,快来申请吧~
前端·github
ak啊1 小时前
WebGL魔法:从立方体到逼真阴影的奇妙之旅
前端·webgl
hang_bro2 小时前
使用js方法实现阻止按钮的默认点击事件&触发默认事件
前端·react.js·html
用户90738703648642 小时前
pnpm是如何解决幻影依赖的?
前端
树上有只程序猿2 小时前
Claude 4提升码农生产力的5种高级方式
前端
傻球2 小时前
没想到干前端2年了还能用上高中物理运动学知识
前端·react.js·开源
咚咚咚ddd2 小时前
前端组件:pc端通用新手引导组件最佳实践(React)
前端·react.js