JavaScript Inject Extra Site Infomation to Copy-Pasted Text

You may have noticed how some popular websites will attach a “read more” link to any text that is copied from their site. This is a great way to get new users. Often folks are copying and pasting text from websites into email. Here is going to explain how to add read more link or copyright information to the bottom of the selected content who is trying to copies contents from your website.

Basically what it does is creating an invisible DOM-element and filling it with the html-code being copied (adding the extra site information).

  • Event copy | oncopy used – It returns the onCopy event handler code on the current element.
  • Function window.getSelection() called – It returns a selection object representing the range of text selected.

Pure JavaScript:

With JQuery:

WordPress Plugin:

What need to do is copy the following code into your theme’s functions.php file (Appearance > Editor > Theme Functions – functions.php) or a site-specific plugin. If you are using a link shortening service, then you can replace get_permalink with wp_get_shortlink.

Now, whenever someone tries to copy your content a read more link will be included along with it. Don’t forget to change the copyright text, and add your own site’s title there.

Leave a Reply

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