7b2主题强化-复制文字自动添加版权信息的方法

我们浏览一些名气较重的博客或网站时常常会发现,对于复制下来的内容往往包含有一些版权声明或推荐阅读之类的链接,这对属于自己博客原创内容版权保护意识比较强且较重视访问流量的朋友来说,这是一条很值得应用的技巧。 我们也可以在WordPress博客上应用这个方法,即使不用插件也能够轻易做到。方法很简单,只需将以下代码添加到当前主题文件的functions.php中即可:

function add_copyright_text() { ?>
<script type='text/javascript'>
function addLink() {
    var body_element = document.getElementsByTagName('body')[0];
    var selection;
    selection = window.getSelection();
    var pagelink = "<br /><br /> 转载请注明来源,本文来自: <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_footer', 'add_copyright_text');

相关文章