俗話說“天下網站一半抄”,網上的博客文章常常被轉載了無數次,雖然寫博客沒收入,我想寫文章也應該是有版權的。
盡管博客文章下面常常加了“轉載請申請來源”的版權信息,但是有些拿來主義者就是“懶得抽筋”,直接復制粘貼不加版權申明。
下面的代碼是針對懶得抽筋的人設計的,是使用JavaScript實現文章復制時,在復制內容里自動添加版權信息的功能,js實現wordpress文章復制自動加版權信息,下面是具體教程:
wordpress主題選項加入自js代碼
你可以將下面代碼加進網頁尾部,如果你的主題沒有加入自定義代碼的功能,你可以直接修改模板文章頁面的php文件,也可以參考第二種方法:
<script type='text/javascript'> function addLink() { var body_element = document.getElementsByTagName('body')[0]; var selection; if(window.getSelection){//DOM,FF,Webkit,Chrome,IE10 selection = window.getSelection(); alert("文字復制成功!若有文字殘缺請用右鍵復制\n轉載請注明出處:"+document.location.href); }else if(document.getSelection){//IE10 selection= document.getSelection(); alert("文字復制成功!若有文字殘缺請用右鍵復制\n轉載請注明出處:"+document.location.href); }else if(document.selection){//IE6+10- selection= document.selection.createRange().text; alert("文字復制成功!若有文字殘缺請用右鍵復制\n轉載請注明出處:"+document.location.href); }else{ selection= ""; alert("瀏覽器兼容問題導致復制失敗!"); } 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.body.oncopy = addLink; </script>
Functions.Php中加入Js腳本函數
function add_copyright_text() { ?> //把方法1的js代碼復制粘貼到此處 <;?php } add_action( 'wp_footer', 'add_copyright_text');