分享学习记录
互联网技术知识

WordPress文章实现禁止复制的两种代码方法

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

第一种方法:

在WordPress我们后台进入到外观主题编辑器,找到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(),  false );
}

if (! current_user_can('level_10') ) {
add_action( 'wp_enqueue_scripts', 'copyrightpro_scripts' );
}
赞(0)
文章名称:《WordPress文章实现禁止复制的两种代码方法》
文章链接:https://www.bailuze.com/15761.html
本站所有文章,如无特殊说明或标注,均为本站原创发布。任何在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
本站专注于百度、搜狗、360、谷歌、bing等常见搜索引擎的优化,关键词排名的提高,诚意咨询邮箱526009505@qq.com
分享到