WordPress复制内容自动添加版权

复制内容自动添加版权这功能虽然不能防止别人复制自己网站的文章,但是至少可以给他一个注明版权的提醒。而且一般大家看到比较好的文章都会存到QQ空间来分享给家人或朋友,如果自动添加版权的话还可以给网站带来一些流量。那WordPress系统的网站如何来实现,添加以下代码即可:

方法一:复制以下代码到主题header.php(全站),或添加到singel.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>

方法二:复制下面的代码到主题的functions.php文件中:

function i_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', 'i_copyright');
展开评论