How to Add Link when anyone Copy text from Website

Automatically add website’s link as reference link when anyone copy text from your website. So i have simple JavaScript code for your, Paste given code on footer then apply.

How Does it Works

If any one copy any text like (hello it thekroyaard website). then automatically add your post’s link like (hello it thekroyaard website www,domain.com)

How To Do

Open your website’s theme, Then paste given on footer section Then SAVE.

Un-minify code

<script>
function tkreferencelink() {
var selection = window.getSelection();
var htmlDiv = document.createElement("div");
for (var i = 0; i < selection.rangeCount; ++i) {
htmlDiv.appendChild(selection.getRangeAt(i).cloneContents());
}
var selectionHTML = htmlDiv.innerHTML;
var pagelink = "<br/> Copied from TheKroyaard : <br/>" + document.location.href;
var copytext = selectionHTML + pagelink;

var newdiv = document.createElement('div');
newdiv.style.position = 'absolute';
newdiv.style.left = '-99999px';

document.body.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function () { document.body.removeChild(newdiv); }, 0);
}
document.oncopy = tkreferencelink
</script>

Minified code

<script>function tkreferencelink(){for(var e=window.getSelection(),n=document.createElement("div"),t=0;t<e.rangeCount;++t)n.appendChild(e.getRangeAt(t).cloneContents());var o=n.innerHTML,r="<br/> Copied from TheKroyaard : <br/>"+document.location.href,i=document.createElement("div");i.style.position="absolute",i.style.left="-99999px",document.body.appendChild(i),i.innerHTML=o+r,e.selectAllChildren(i),window.setTimeout(function(){document.body.removeChild(i)},0)}document.oncopy=tkreferencelink;<script>

Using functions.php

When you want to this code work only on single.php so paste given on functions.php and SAVE.

function add_reference_link() {
if (is_single()) {
?>
<script> function tkreferencelink() { var selection = window.getSelection(); var htmlDiv = document.createElement("div"); for (var i = 0; i < selection.rangeCount; ++i) { htmlDiv.appendChild(selection.getRangeAt(i).cloneContents()); } var selectionHTML = htmlDiv.innerHTML; var pagelink = "<br/> <br/> Copied from TheKroyaard : <br/>" + document.location.href; var copytext = selectionHTML + pagelink; var newdiv = document.createElement('div'); newdiv.style.position = 'absolute'; newdiv.style.left = '-99999px'; document.body.appendChild(newdiv); newdiv.innerHTML = copytext; selection.selectAllChildren(newdiv); window.setTimeout(function () { document.body.removeChild(newdiv); }, 0); } document.oncopy = tkreferencelink </script>
<?php
}
}

add_action( 'wp_head', 'add_reference_link');

Paste any one code on your website, Hope this code will work perfectly. Thanks for visiting.