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

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

作者 : 资源客 发布时间: 2019-11-14

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

方法一

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

    <script>
    // 禁止右键
    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(), version, false );
    }
 
    if (! current_user_can('level_10') ) {
    add_action( 'wp_enqueue_scripts', 'copyrightpro_scripts' );
    }

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

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

资源客 » 禁止WordPress图片拖放文章选择防复制