首页 >  帮助中心 > wordpress文章被复制的时候如何自动带链接?

wordpress文章被复制的时候如何自动带链接?

来源:原创 点击量(251) 发布时间:2019-10-08

辛辛苦苦写的文章被人给采集了!想想自己辛辛苦苦写的文章被偷走了,而且对方居然收录比你好,你说气人不气人,所以想查了一些防止被人采集的方法,总得来说都不大理想,比如说禁止复制之类的,对一个技术站来说,禁止复制是相当影响用户体验的,所以一直没弄。想了下在不影响用户体验的情况下,只有在文章被复制的时候自动加链接最为温和。

方法很简单,进入wordpress主题编辑器

找到functions.php函数模板文件,编辑,加入以下代码保存即可:

function add_copyright_text() {
    if (is_single()) { ?>
 
<script type='text/javascript'>
function addLink() {
    if (
window.getSelection().containsNode(
document.getElementsByClassName('entry-content')[0], true)) {
    var body_element = document.getElementsByTagName('body')[0];
    var selection;
    selection = window.getSelection();
    var oldselection = selection
    var pagelink = "<br /><br /> 阅读更多: <?php the_title(); ?> <a href='<?php echo get_permalink(get_the_ID()); ?>'><?php echo get_permalink(get_the_ID()); ?></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', 'add_copyright_text');


  • 服务热线   4006-598-598