最近主機(jī)吧博客遇到一件很惡心的事情,辛辛苦苦寫的文章被人給采集了!連標(biāo)題都沒(méi)改,想想自己辛辛苦苦寫的文章被偷走了,而且對(duì)方居然收錄比你好,你說(shuō)氣人不氣人,所以主機(jī)吧在舉報(bào)的同時(shí)也想查了一些防止被人采集的方法,總得來(lái)說(shuō)都不大理想,比如說(shuō)禁止復(fù)制之類的,對(duì)一個(gè)技術(shù)站來(lái)說(shuō),禁止復(fù)制是相當(dāng)影響用戶體驗(yàn)的,所以主機(jī)吧一直沒(méi)弄。想了下在不影響用戶體驗(yàn)的情況下,只有在文章被復(fù)制的時(shí)候自動(dòng)加鏈接最為溫和。方法很簡(jiǎn)單,
進(jìn)入wordpress主題編輯器
找到functions.php函數(shù)模板文件,編輯,加入以下代碼保存即可:
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>"; //根據(jù)你的需要修改這行代碼 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');