Automatically add source URL copyright notice to copied text if greater than a paragraph

Like every content creator you update your WordPress website regularly with new fresh articles and pages, so you want your effort to be indexed first by Google as soon as possible. And also you want to prevent others from copying your content or add at least they need to give you a credit which is the original source URL.

In the previous articles about WordPress tutorial, There’s one I showed you how to add watermark to your WordPress images and internal links which is really effective to protect your copyrights. Let’s now enhance it by automatically adding source URL copyright notice to copied text if it is greater than a paragraph.

This way you still asks the copiers to give you credits and a natural backlink but also don’t block regular readers from selecting a quote or a link. Follow the steps below:

Inside your WordPress admin dash board, go to Appearance ยป Theme File Editor

In the right side of the screen, under Theme Files, find header.php and click on it

WordPress automatically add source url credit to copied text theme file header
WordPress automatically add source url credit to copied text theme file header

As shown in the image , now in header.php, insert the below code to above </head>

<script type="text/javascript">
		function addLink() {
			var body_element = document.getElementsByTagName('body')[0];
			var selection;
			selection = window.getSelection();
			const words = selection.toString().split(" ");

			// if words copied < 40 about a paragraph then we don't add the copyright notice
			if (words.length < 40) {
				return;
			}
			
			var pagelink = `<br /><br /> Read more at: <a href='${document.location.href}'> ${document.location.href}</a> <br />Copyright @ 2022 Mayviti Computer & Mobile Tutorial`; // You can change this to your copyright notice
		  var copytext = selection + pagelink;
		  var newdiv = document.createElement('div');
		  newdiv.style.position = 'absolute';
		  newdiv.style.left = '-99999px';
		  body_element.appendChild(newdiv);
		  newdiv.innerHTML = copytext;
		  selection.selectAllChildren(newdiv);
		  window.setTimeout(function () {
			body_element.removeChild(newdiv);
		  }, 0);
		}
		document.oncopy = addLink;
	</script>

There in the code, you see a line says if (words.length < 40) meaning if the system detects if less than 40 words selected and copied then it’s regular users trying to copy a quote or a link otherwise greater than or equal to 40 then someone is copying your content. Change the number to fit your situation.

Now click Update File

Theme file header.php added source url credit code
Theme file header.php added source url credit code

Go to any of your WordPress articles, copy and paste any text

WordPress automatically add source url credit to copied text successfully
WordPress automatically add source url credit to copied text successfully

Leave a Reply

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