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'); |
Ad Blocking Detected Please consider disabling your ad blocker for our website.
We rely on these ads to be able to run our website.
You can of course support us in other ways (see Support Us on the left).
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:
Comments
There are 8 comments. You can read them below.
You can post your own comments by using the form below, or reply to existing comments by using the "Reply" button.
Thank you
grk
Thanks GRK for taking the time to post a thank-you note – it’s very much appreciated!
hans
Dear hans,
Thank you very much, the code works!
Pretty cool.
:-)
Theme Cat
Awesome!
Thanks Theme Cat for posting a Thank-You note – it’s much appreciated!
hans
Worked like a charm!
Thank you for sharing!
Aristotelis
Hi Aristotelis!
Thank you for taking the time to post a thank-you! It is much appreciated!
Hans
Nice article 👍
Tiya
Thanks
Hans