參考文章

只要在function.php貼上以下代碼即可

//當有人複製網站文字時,會自動出現文章標題+原文網址
function add_copyright_text() {
    if (is_single()) {
        ?>
        <script type='text/javascript'>
            function addSourceLink() {
                var body_element = document.getElementsByTagName('body')[0];
                var selectedText = window.getSelection();
                var sourcelink = "<br \><br \>原文網址 <a href='<?php the_permalink(); ?>'><?php wp_title(); ?></a> (<?php the_permalink(); ?>)</p>";
                var newText = selectedText + sourcelink;
                var newDiv = document.createElement('div');
                    newDiv.style.left='-99999px';
                    newDiv.style.position='absolute';

                body_element.appendChild(newDiv);
                newDiv.innerHTML = newText ;
                selectedText.selectAllChildren(newDiv);
                window.setTimeout(function() { body_element.removeChild(newDiv); } ,0);
            }
            document.oncopy = addSourceLink;
        </script>
    <?php
   }
}

add_action( 'wp_head', 'add_copyright_text');
最後修改日期: 2022 年 3 月 16 日

作者