WordPress教程

禁止WordPress图片拖放文章选择防复制

阿里云

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

方法一

打开当前主题头部模板 header.php 找到:将下面代码添加到后面:

也想出现在这里?联系我们
创客主机
  1.     <script>
  2.     // 禁止右键
  3.     document.oncontextmenu = function() {
  4.     return false
  5.     };
  6.     // 禁止图片拖放
  7.     document.ondragstart = function() {
  8.     return false
  9.     };
  10.     // 禁止选择文本
  11.     document.onselectstart = function() {
  12.     if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password") return false;
  13.     else return true;
  14.     };
  15.     if (window.sidebar) {
  16.     document.onmousedown = function(e) {
  17.     var obj = e.target;
  18.     if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD") return true;
  19.     else return false;
  20.     }
  21.     };
  22.     // 禁止 frame 标签引用
  23.     if (parent.frames.length > 0) top.location.replace(document.location);
  24.     </script>

方法二

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

  1.     // 禁止右键
  2.     document.oncontextmenu = function() {
  3.     return false
  4.     };
  5.     // 禁止图片拖放
  6.     document.ondragstart = function() {
  7.     return false
  8.     };
  9.     // 禁止选择文本
  10.     document.onselectstart = function() {
  11.     if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password") return false;
  12.     else return true;
  13.     };
  14.     if (window.sidebar) {
  15.     document.onmousedown = function(e) {
  16.     var obj = e.target;
  17.     if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD") return true;
  18.     else return false;
  19.     }
  20.     };
  21.     // 禁止 frame 标签引用
  22.     if (parent.frames.length > 0) top.location.replace(document.location);

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

  1.     function copyrightpro_scripts() {
  2.     wp_enqueue_script( 'copyright', get_template_directory_uri() . '/copyright.js', array(), version, false );
  3.     }
  4.  
  5.     if (! current_user_can('level_10') ) {
  6.     add_action( 'wp_enqueue_scripts', 'copyrightpro_scripts' );
  7.     }

代码中加了判断,管理员登录状态下防复制代码无效。当然上面的方法,也只是忽悠一下小白,浏览器禁用 JavaScript 后,将失去效果。如果你不喜欢折腾代码的话,可以直接安装该功能的CopyRightPro插件

禁止 WordPress 图片拖放文章选择防复制

已有 333 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!