最近逛某 XX 資訊網站時發現複製該網站的內容時會自動添加一個版權,小編覺得這功能不錯雖然不能防止別人複製自己網站的文章但是至少可以給他一個註明版權的提醒。而且一般大家看到比較好的文章都會存到 QQ 空間來分享給家人或朋友,如果自動添加版權的話還可以給網站帶來一些流量。那麼 WordPress 系統的網站如何來實現這點呢?下面小編給出兩個實現 WordPress 文章被複制添加版權方法:

方法一:複製下面的代碼到主題的 functions.php 文件中

function wxd_copyright() { ?>
<script type='text/javascript'>
function addLink() {
    var body_element = document.getElementsByTagName('body')[0];
    var selection;
    selection = window.getSelection();
    var pagelink = "<br /><br /> 原文信息: <?php if(is_single()){ the_title();}?> 原文鏈接:<a href='"+document.location.href+"'>"+document.location.href+"</a>";
    var copy_text = selection + pagelink;
    var new_div = document.createElement('div');
    new_div.style.left='-99999px';
    new_div.style.position='absolute';
    body_element.appendChild(new_div );
    new_div.innerHTML = copy_text ;
    selection.selectAllChildren(new_div );
    window.setTimeout(function() {
        body_element.removeChild(new_div );
    },0);
}
document.oncopy = addLink;
</script>

<?php
}
add_action( 'wp_head', 'wxd_copyright');

方法二:複製以下代碼到到主題的 header.php 文件中

<script type='text/javascript'>
function addLink() {
    var body_element = document.getElementsByTagName('body')[0];
    var selection;
    selection = window.getSelection();
    var pagelink = "<br /><br /> 原文信息: <?php if(is_single()){ the_title();}?> 原文鏈接:<a href='"+document.location.href+"'>"+document.location.href+"</a>";
    var copy_text = selection + pagelink;
    var new_div = document.createElement('div');
    new_div.style.left='-99999px';
    new_div.style.position='absolute';
    body_element.appendChild(new_div );
    new_div.innerHTML = copy_text ;
    selection.selectAllChildren(new_div );
    window.setTimeout(function() {
        body_element.removeChild(new_div );
    },0);
}
document.oncopy = addLink;
</script>