Page 1 of 1

WordPress – Copy & Paste with link back to your website

WordPress – Copy & Paste with link back to your website
   0

For those of you writing articles or blog posts, you’ll find that it takes a lot of work to write a good article. Even a tiny post can take up a lot of time.

And what is the most annoying part? Folks just bluntly copying your work, without even asking or giving credit (there are a very few exceptions). It seems that the majority on the Internet is replicated that way. Copy, and Paste.

For WordPress users (and if you only take out the JavaScript part, this might work for others as well) can, with a little trick, automatically add a “source” link below what has been copied. Not only will this be a reminder for the “copier”, but (if left intact) a good way to boost SEO for your website.




JavaScript, WordPress and Copy & Paste

This function has to be added to your theme’s “functions.php” file.

The only thing it does, is add a JavaScript to the header of each WordPress page, and in this particular example, only for pages that display posts.

The JavaScript (see sources below) basically catches a “Copy” event and adds a link to the original page at the bottom.
You can format that anyway you’d like, just modify line 8 (the highlighted line).

To see this in action, just copy a piece of this article and paste it in (for example) a Word Document, or an eMail, and you’ll see the effect.

A few notes on the JavaScript code

In line 8, I initially used the paragraph tag (<p>...</p> ), to find out that this works great for HTML and RichText editors. But does not work for plain text editors.

This (plain text editors) is also the reason why the link is added twice so it still remains when pasting it in a plain text editor – once as the title of your post which is clickable, and once as text incase the link disappears from the title.

In other examples (see below) I have seen that only one specific CSS will be copied this way, which I found to not always work as expected, even if you find out which class you’d want.

Again: if you’d like to change the “source” link added, then you only have to change line 8.

Some users prefer using “wp_get_shortlink(get_the_ID())” instead of “the_permalink()“, to keep the link shorter and maybe cleaner. Personally I prefer a readable link, but to each it’s own.

For those complaining that this type of code is annoying, well, stop copying content … 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 \>Source: <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');

 

Sources

In this case, I didn’t invent the wheel myself, and learned a lot of similar solutions and modifed examples to my taste.
Most I have learned from WPBeginner.com, definitely a great resource for WordPress users!

Bavotasan’s post (from 2010!) is a great resource for non-WordPress users, and has been the first/oldest post I could find (please correct me if I’m wrong).

For WordPress specific use:

For generic use:

Donation options


Donations are very much appreciated, but not required. Donations will be used for web-hosting expenses, project hardware or a motivational boost (a drink or snack). Thank you very much for those have donated already! It's truly AwEsOmE to see that folks like our articles and small applications.

Comments


There are no comments yet.
You can post your own comments by using the form below, or reply to existing comments by using the "Reply" button.



Your Comment …

Friendly request to not post large files here (like source codes, log files or config files). Please use the Forum for that purpose.

Please share:
*
*
Notify me about new comments (email).
       You can also use your RSS reader to track comments.


Tweaking4All uses the free Gravatar service for Avatar display.
Tweaking4All will never share your email address with others.