WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Tutorials» How To Add a Read More Link to Copied Text in WordPress

How To Add a Read More Link to Copied Text in WordPress

Last updated on June 13th, 2012 by Editorial Staff
58 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
How To Add a Read More Link to Copied Text in WordPress

You may have noticed how some popular websites (like eHow etc) 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. Some use it to save the information for future, while others are just sharing it with their friends, family, and colleagues. In this article we are going to show you how you can easily add a “read more” link to any copied text from your WordPress blog, so you can get additional traffic.

Note: To see this in action, copy anything on this post and paste it in your email or notepad.

First thing you need to do is copy the following code into your theme’s functions.php file or a site-specific plugin:


function add_copyright_text() {
	if (is_single()) { ?>

<script type='text/javascript'>
function addLink() {
	if (
window.getSelection().containsNode(
document.getElementsByClassName('entry-content')[0], true)) {
    var body_element = document.getElementsByTagName('body')[0];
    var selection;
    selection = window.getSelection();
	var oldselection = selection
    var pagelink = "<br /><br /> Read more at WPBeginner: <?php the_title(); ?> <a href='<?php echo wp_get_shortlink(get_the_ID()); ?>'><?php echo wp_get_shortlink(get_the_ID()); ?></a>"; //Change this if you like
    var copy_text = selection + pagelink;
    var new_div = document.createElement('div');
	new_div.style.left='-99999px';
	new_div.style.position='absolute';

    body_element.appendChild(new_div );
    new_div.innerHTML = copy_text ;
    selection.selectAllChildren(new_div );
    window.setTimeout(function() {
        body_element.removeChild(new_div );
    },0);
}
}


document.oncopy = addLink;
</script>

<?php
}
}

add_action( 'wp_head', 'add_copyright_text');

Now, whenever someone comes to your website and 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.

Edit: Initially we were grabbing the window URL, but as one of our users pointed out that it made the URLs look clunky. We have updated the code to only show it on single post pages, and use wp_shortlink. So it would use something like this: yoursite.com/?p=23. If you are using a customized shortlink like we are: wpbeg.in, then it will show the branded shortlink.

58 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • How to Fix the Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

  • Revealed: Why Building an Email List is so Important Today (6 Reasons)

    Revealed: Why Building an Email List is so Important Today (6 Reasons)

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

57 Comments

Leave a Reply
  1. Edward Elric says:
    Nov 23, 2020 at 8:58 pm

    Is this still working in 2020 with latest WordPress?

    Reply
    • WPBeginner Support says:
      Nov 24, 2020 at 10:32 am

      Unless we hear otherwise, the code should still work when added to your site.

      Reply
  2. Junuk says:
    Jun 4, 2019 at 1:25 am

    Hi, Thank you for your good information!

    I applied the code you have told me with BItly plugin to my website, but it works sometimes and doesn’t work sometimes.

    Does this code have relation with Cache plug-in? Can you tell me where in my website I should take a look?

    Reply
    • WPBeginner Support says:
      Jun 4, 2019 at 11:42 am

      If you have a caching plugin enabled, you would likely need to clear all caching on your site for it to update properly. For clearing your cache you may want to take a look at our guide here: https://www.wpbeginner.com/beginners-guide/how-to-clear-your-cache-in-wordpress/

      Reply
  3. Pierre says:
    Apr 4, 2019 at 10:35 pm

    Hi,

    Thanks, it’s work fine on article on 2019, but it’s possible to make it work on custom post type?

    I have Eventon and WP News and Scrolling Widgets.

    Reply
    • WPBeginner Support says:
      Apr 5, 2019 at 11:18 am

      You would need to modify the is_single to is_singular and add the name of your custom post type

      Reply
  4. Will says:
    Apr 29, 2017 at 8:07 am

    Do you have any advice on how to add Google tracking parameters to the end of each URL? Considering you’re already using ?p= for the post name at the end.

    Reply
  5. Akash says:
    Dec 19, 2016 at 11:40 pm

    Is there an easy way to make it work on categories, too?

    Reply
  6. Jasmine says:
    Jun 24, 2016 at 7:07 am

    Thanks for this, it works great for posts but when text is copied from events it doesn’t pull the URL of the page through – any advice? Thanks.

    Reply
    • Michele says:
      Apr 18, 2017 at 2:45 pm

      This is awesome. Thanks. When it says class – entry content, that meant it didn’t work for about 90% of the text on my site because my text didn’t use that class name. So if anyone else encounters this problem, you can change “entry content” to “body”. That’s what I did.
      var body_element = document.getElementsByTagName(‘body’)[0]; (line 13)
      Now it works everywhere.

      Reply
  7. Meenu says:
    Mar 16, 2016 at 3:38 am

    How can I limit the content?

    Reply
  8. Dave says:
    Mar 15, 2016 at 7:55 am

    Thank you very much, all the text formatting were changed and pasted as a single line. But I need the text formatting as it is. So please help me in getting this.

    Reply
    • Ark says:
      Mar 31, 2016 at 3:26 am

      Same here, all line breaks are missing after the paste.

      Reply
  9. Jens | Sir Apfelot says:
    Jan 28, 2016 at 1:09 pm

    Damn… I am stupid… I added it to the themes custom JS… thats not working. :-)

    Reply
  10. Jens | Sir Apfelot says:
    Jan 28, 2016 at 1:07 pm

    Just added the script into my wordpress blog, but I dont get an appendix at the clipbiards end…

    Any ideas? I am not sure, if its because I am working on a mac… but I guess the javascript should work on Firefox on a mac too… :-(

    Reply
  11. Yash Jhade says:
    Dec 28, 2015 at 2:32 am

    While pasting it to blogger following error comes
    Error parsing XML, line 2237, column 96: The value of attribute “href” associated with an element type “a” must not contain the ‘<' character.
    Any Solutions ?
    Thanks.

    Reply
  12. matt says:
    Nov 11, 2015 at 2:18 pm

    this feature makes copying and pasting more frustrating. For instance, if I copy and paste a headline for editing Wikipedia it can mess up formatting if I’m not careful and requires extra work.

    Reply
  13. Anon says:
    Aug 28, 2015 at 11:11 pm

    As a user, I can’t stand this. Please don’t add this.

    Reply
  14. Alan says:
    Apr 21, 2015 at 3:37 pm

    Anybody knows how to keep the format?

    When using this plugin all the spaces, line break and format goes away.

    Reply
  15. Philip-Daniel Kleudgen says:
    Oct 17, 2014 at 10:02 am

    Works like a charm! The only thing i wish it would do is limit the amount of copied text to for example 150 words or something like that.

    Reply
  16. Salud Casera says:
    Jun 28, 2014 at 8:56 pm

    No longer works for me.

    It was working before though but I think it has more to do with the theme rather than the WP version

    Reply
  17. Alyson says:
    Dec 21, 2013 at 7:26 pm

    Unfortunately, the code does not work with WP 3.8

    I have tested it on a fresh install. Please feel free to confirm this. Do you have any plans to update the code to make it function?

    Reply
    • WPBeginner Support says:
      Dec 22, 2013 at 3:44 pm

      We just tested it with a fresh WP 3.8 install and it is working perfectly on our side.

      Reply
      • Erik says:
        Jan 10, 2014 at 4:00 pm

        I can’t get this to work either actually.

        Reply
    • Roselle says:
      Mar 20, 2014 at 2:05 pm

      Hello,

      At first, I could not get this to work. But upon close examination, I figured out the culprit:

      On the 8th line of the code, you’ll see this:
      document.getElementsByClassName(‘entry-content’)[0], true))

      This is the line that tells the code, “hey, if anyone copies text from text with this class name, then put a read more link”

      All you need to do is use Firebug to figure out what class name your post content is under.

      I have a child theme that I’ve edited over and over again, so mine wasn’t the default, “entry-content”. I suspect your class name is different from that also.

      Good luck,
      Roselle

      Reply
      • Salud Casera says:
        Jun 28, 2014 at 8:58 pm

        What are the steps to follow with Firebug to figure out the post class name?

        Reply
  18. Gabriel says:
    Jun 16, 2013 at 2:46 pm

    Hello,

    Many thanks for this great article, but I have a problem. I would like to get the permalink and not the shorthen link. Anyone can tell me how can I do that?

    Cheers

    Reply
    • Editorial Staff says:
      Jun 16, 2013 at 10:14 pm

      Replace echo wp_get_shortlink(get_the_ID()); with the_permalink();

      Reply
  19. Gretchen Louise says:
    Jan 11, 2013 at 7:51 pm

    Is there an easy way to make it work on single pages, too?

    Reply
    • Editorial Staff says:
      Jan 12, 2013 at 5:29 am

      Yes. Change line 2 code from:

      if (is_single()) to if (is_singular())

      Reply
      • Gretchen Louise says:
        Jan 12, 2013 at 10:43 am

        Thank you! That works, except it doesn’t grab a link for pages…just posts.

        Reply
        • Editorial Staff says:
          Jan 16, 2013 at 12:48 pm

          That’s weird. get_the_id should work for both pages and posts.

        • Gretchen Louise says:
          Jan 16, 2013 at 12:57 pm

          I have it workign on KindredGrace . com but it doesn’t grab the id for pages, just posts.

        • Editorial Staff says:
          Jan 16, 2013 at 3:05 pm

          Try adding the global variable. Because get_the_id works for posts and pages are also a “post type”

      • Gretchen Louise says:
        Jan 16, 2013 at 6:10 pm

        I tried it like this and still no luck on grabbing the shortlink for pages:

        // Add copyright text
        function add_copyright_text() {
        global $post; { ?>

        Reply
      • Brian York says:
        Apr 29, 2014 at 8:32 am

        This change worked for me. However do double check that you have your content under ‘entry-content’ otherwise change that (F12 in Chrome). Thanks WPBeginner!!

        Reply
      • Brian York says:
        Apr 29, 2014 at 9:15 am

        Actually this doesn’t quite work right. It doesn’t work in IE at all and no matter what I do its not copying the HTML part but always converting it to plain text thus would never make a link.

        Reply
        • alan says:
          Apr 21, 2015 at 3:36 pm

          I have the same problem, it removes all the format, does anybody knows a fix to this?

  20. VIVROCKS says:
    Dec 10, 2012 at 4:19 am

    Great Trick. there is plugin too for this: http://wordpress.org/extend/plugins/read-more-copy-link/

    Reply
  21. Collin Weeks says:
    Aug 10, 2012 at 9:24 pm

    Love your website! Thanks for taking the time to share with everyone.

    Reply
  22. Mairaj Pirzada says:
    Jun 28, 2012 at 6:18 am

    Nice, Post but I always don’t like Codings, it confuses me!

    Reply
  23. Helder says:
    Jun 20, 2012 at 4:18 pm

    It´s not working, even with wpbeginner.com content

    Reply
    • Editorial Staff says:
      Jun 20, 2012 at 4:50 pm

      We took the code off our website. Some users complained that they found it annoying.

      Reply
  24. Rafiul Islam says:
    Jun 14, 2012 at 6:43 am

    WOW… Many Many Thanks for share it,, I am suffering for this …
    Thanks again.

    Reply
  25. Lorenzo Solís says:
    Jun 13, 2012 at 3:59 am

    Does someone know how to do not show the “Read More Link” when the post title is copied?

    Thanks!

    Reply
    • Editorial Staff says:
      Jun 13, 2012 at 9:56 am

      Yes it is possible. Change the selector tag from body to a div ID or what not. I have put the request in. Our developers will let me know, so I can update the article.

      Reply
      • Lorenzo Solís says:
        Jun 13, 2012 at 11:40 am

        Thanks. I’ve just tried with var div = document.getElementById(‘copyright’); in the line number 6 and adding de id=”copyright” to the div content, but I get the same result as with your code. Sorry, I am not a programmer.

        Reply
        • Editorial Staff says:
          Jun 13, 2012 at 5:13 pm

          Check out the code now. It should work.

  26. Paul says:
    Jun 12, 2012 at 9:06 pm

    Thanks, this looks interesting I’ll be trying it out soon

    Reply
  27. Lisa says:
    Jun 12, 2012 at 10:54 am

    It also worked fine in a new email composed from Outlook, so I’d say it’s a Gmail issue. Thanks!

    Reply
    • Editorial Staff says:
      Jun 12, 2012 at 11:59 am

      Just tried it with Gmail.. It works fine here.

      Reply
  28. Lisa says:
    Jun 12, 2012 at 10:53 am

    Interesting and useful; however, I was surprised that the copy test did not work (for me at least) when composing an email in Gmail — nothing showed after pasting the copied text, and the body text area in the compose email window just froze. Testing a copied bit in Notepad worked just fine, so I don’t know what the issue might be with Gmail.

    Thanks for the tip!

    Reply
  29. Albert Albs says:
    Jun 12, 2012 at 7:33 am

    That is good option Syed. Thanks for sharing. But if we can able to print short URL of the post means, that is even better for website owners.

    Currently it is printing long URL. If I came here by Feedburner newsletter subscription, the UTM tracking url also coming.

    Hope you understand. Is it possible?

    Reply
    • Editorial Staff says:
      Jun 12, 2012 at 12:00 pm

      Ah hah. Good suggestion. Writing it down in the list of to-do things for WPBeginner.

      Edit: Just updated the code :)

      Reply
      • Albert Albs says:
        Jun 12, 2012 at 1:46 pm

        That is fast and smart.

        One Question: Why you removed Author URL in the comment form? Always, I’m seeing, many of the WordPress powered blogs will give credit to the comment author at URL field with NoFollow attribute.

        Is there any specific reason for removing Author URI field? Or are there any experiments for Penguin update?

        Between, I’m not getting any comment notification, even after selecting the “Notify me of followup comments via e-mail” option.

        Reply
  30. Gabe Diaz says:
    Jun 11, 2012 at 11:17 am

    Wow, now this is a great tip! Makes it easy to quote and show attribution as well as drops a link for those who skim and steal content. Very cool!

    Reply
  31. Emily Sandford says:
    Jun 11, 2012 at 10:24 am

    Thanks so much for this. It was so easy to do, and something I wondered about for quite some time. Appreciate it!

    Reply

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
PushEngage
PushEngage
Increase your website traffic & revenue with push notifications. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2020 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2020)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2020)
    • SiteGround Reviews from 4196 Users & Our Experts (2020)
    • Bluehost Review from Real Users + Performance Stats (2020)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2020 – Step by Step Guide
Deals & Coupons (view all)
Webnus
Webnus Coupon
Get 20% OFF on Webnus themes and plugins for WordPress.
PanKogut
PanKogut Coupon
Get 20% OFF on PanKogut's premium WordPress themes collection.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).
Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress CDN by MaxCDN | WordPress Security by Sucuri.