Blog / WordPress/ How to Add a Copyright Notice to WordPress Posts Without Plugins (Updated)

How to Add a Copyright Notice to WordPress Posts Without Plugins (Updated)

WordPress 无插件实现文章末尾自动添加转载版权声明( 更新版)

Add a Copyright Notice at the End of WordPress Posts Without Plugins

To protect original content, many site owners want to automatically add a copyright notice at the end of their posts. Without using plugins, you can achieve this by modifying your theme template files or adding JavaScript. Here are two main methods.

Method 1: Modify the Theme Template File (single.php)

This method edits the single post template to output the notice after the post content. In your theme's single.php file, locate the function that outputs the post content (usually the_content()) and add the following code after it:

<!-- Copyright Notice Start -->
<div class='repost-notice'>
    <p>Original: <a rel='bookmark' title='<?php the_title(); ?>' href='<?php the_permalink(); ?>'>“<?php the_title(); ?>”</a></p>
    <p>Reprinted from: <a href='<?php echo home_url(); ?>'><?php bloginfo('name'); ?></a></p>
</div>
<!-- Copyright Notice End -->

Code Explanation & Updates:

  • The outdated get_settings('home') has been replaced with the modern home_url() function.
  • Added a <div class='repost-notice'> container for easier CSS styling.
  • Improved HTML structure by using paragraph tags (<p>) instead of line breaks (<br />) for better semantics.

Method 2: Add Copyright Automatically When Content is Copied (JavaScript)

This method automatically adds copyright information to the clipboard text when a user copies content. Add the following code to your theme's footer.php file or via the WordPress Custom HTML widget.

<!-- Copy with Copyright Start -->
<script type='text/javascript'>
    document.body.oncopy = function(event) {
        event.preventDefault();
        var selectedText = window.getSelection().toString();
        if (!selectedText) return;

        var blogName = '<?php echo esc_js(get_bloginfo("name")); ?>';
        var blogUrl = '<?php echo esc_js(home_url()); ?>';
        var postUrl = '<?php echo esc_js(get_permalink()); ?>';

        var copyrightText = 'nnSource: ' + blogName + ' (' + blogUrl + '), Original URL: ' + postUrl + 'n';

        try {
            navigator.clipboard.writeText(selectedText + copyrightText).then(function() {
                console.log('Copied with copyright info.');
            });
        } catch (err) {
            var clipboardData = event.clipboardData || window.clipboardData;
            if (clipboardData) {
                clipboardData.setData('text/plain', selectedText + copyrightText);
            }
        }
    };
</script>
<!-- Copy with Copyright End -->

Code Explanation & Updates:

  • Updated deprecated document.selection and event.returnValue to modern Web APIs (window.getSelection(), event.preventDefault()).
  • Uses the modern navigator.clipboard API with a fallback for older browsers.
  • Uses esc_js() to escape PHP variables, preventing JavaScript injection.
  • Added a check to avoid triggering when no text is selected.

Important Notes

  1. Backup Theme Files: Always backup files like single.php or footer.php before editing.
  2. Use a Child Theme: Modify files in a child theme to prevent changes from being overwritten during theme updates.
  3. Method 2 Limitations: The JavaScript method relies on the browser Clipboard API, which may not work in all browsers or security settings, especially on mobile. It should be a supplementary measure, not your only copyright protection.

Tip: For more flexible management (e.g., different notices per category) or to avoid code edits, consider a dedicated plugin. However, these plugin-free methods are excellent for lightweight solutions and learning theme development.

Post a Comment

Your email will not be published. Required fields are marked with *.