Get In Touch
113 Cherry St #92768,
Seattle, WA 98104-2205
[email protected]
Back

Add a Copyright Notice to Copied Text in WordPress

Hey y’all!!!

I recently stumbled on a tutorial from Bavotasan, a website filled with lots of articles and resources for web developers. One of the latest was “Add a Copyright Notice to Copied Text”. I found that resourceful as I’d like to add that to my blog so that anyone who copies text from my blog will see my copyright notice. A couple of Lyrics’ websites use this technique also.

You could try copying any text from this website. Then paste it in your text editor. See what is written.

This blog is powered by WordPress and so one of the quickest ways to do this seemlessly is to create a plugin for this function or just add the codes (that we will see) to your theme’s functions.php file.

Briefly, here is what you need to know and what you we will be needing:

  • You will need to take a look at Bavotasan’s code
  • We will be examining the wp_head WordPress hook. Read more about it here.

C. Bavota, specifies that the code needed which is javascript by the way, needs to be put in the <head></head> tags of your web page or in this case, your wordpress header.

I will run through a brief explanation of his codes and then we jump right at how to integrate it with WordPress.

We create a function in javascript called addLink.

addLink has a couple of variables defined. They are:

  • body_element: Body element uses DOM and specifies ‘anything in the body section of your page’.
  • selection: Is the variable definition of what we select from our page.Assigned to it on the next line is window.getSelection(). This function holds what we select.
  • pagelink: This is the link we want our copy and pasters will see that refers them both to the particular webpage that the user copies from and then the copyright information and owner of the text.
  • copytext: This is the final copied content comprising of ‘selection and pagelink’
  • newdiv: Is the html of what our copied text will be.

Very simple and straight forward script actually. The script is ended by calling the document.oncopy DOM function. This is assigned a value of our javascript function: addLink();

Next on, adding this to WordPress. Remember we are meant to put this code in between our tags. This is easily accomplished by using the wp_head WordPress action hook.
So first, you create a function, naming it what ever name you want. In my case, I named it copyrightpaster.

We will pass the javascript file into the function and echo it.
Next, we …hook the script using this filter thankfully provided by wordpress. You do that by typing ‘add_action(‘wp_head’,’copyrightpaster’);

There we go! Copy and paste your code in your theme’s functions.php file and you are good to go!

Here is the final code:


function copyrightpaster(){
	$output = '';



    echo $output;


}

add_action('wp_head','copyrightpaster');


Thanks for reading. And don’t forget to also check related articles with Bovatasan.

9 comments

Leave a Reply

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

This website stores yummy cookies on your computer. Cookie Policy