欢迎光临
我们一直在努力

WordPress文章如何防复制两种代码方法

有的时候我们非常烦恼,我们好不容易辛辛苦苦写的一篇文章,自己的站还没收录呢,却被别人复制走发布到自己的网站上却收录了,反而百度会认为我们是在抄袭对于我们的网站也不太友好,那么WordPress程序如何来防止复制呢?两种方法:

第一种:

在WordPress我们后台进入到外观主题编辑器,找到header.php文件,将以下代码添加到<?php wp_head();?>的后面。

<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(),  false );
}
 
if (! current_user_can('level_10') ) {
add_action( 'wp_enqueue_scripts', 'copyrightpro_scripts' );
}

代码是有管理员判断,如果管理员登录后的话是可以进行复制的。

代码来源于知更鸟;

赞(0) 打赏
未经允许不得转载:新起点博客 » WordPress文章如何防复制两种代码方法


关注公众号『新起点软件管家』

获取最新网络资源及破解软件!
带你玩转各样软件...

评论 1

评论前必须登录!

 

  1. #-19

    不错,必须顶一下!

    自媒体运营2年前 (2021-12-08)

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏