今天给大家分享一个在WordPress主题中,加入复制文章内容时弹出版权提示,提醒转载注明文章出处的小功能。 该功能哈米进行了优化,在剪切板的复制内容后面自动加入了源网站的版权信息,效果可参考本站。 在WordPress主题功能文件functions.php中加入以下代码:
function hm_copyright_tips() { echo '<link rel="stylesheet" type="text/css" rel="external nofollow" target="_blank" href="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.css" rel="external nofollow" rel="external nofollow" >'; echo '<script src="https://cdn.bootcss.com/sweetalert/1.1.3/sweetalert.min.js"></script>'; echo '<script>document.body.oncopy = function() { swal("复制成功!", "撰文不易,转载请务必保留原文链接,申明来源,谢谢合作!","success");};</script>'; ?> <script type='text/javascript'> function addLink() { if (window.getSelection().containsNode(document.getElementsByClassName('single-content')[0], true)) { var body_element = document.getElementsByTagName('body')[0]; var selection = window.getSelection(); var pagelink = "<br/><br/>作者:<?php the_author(); ?><br/>链接:<a href='<?php echo get_permalink(get_the_ID()); ?>'><?php echo get_permalink(get_the_ID()); ?></a><br/>来源:<?php bloginfo('name'); ?><br/>著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。"; 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', 'hm_copyright_tips');