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

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

Last updated on by
Follow WPBeginner on YouTube
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.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • Alyson

    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?

    • http://www.wpbeginner.com/ WPBeginner Support

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

      • Erik

        I can’t get this to work either actually.

    • Roselle

      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

  • Gabriel

    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

    • http://www.wpbeginner.com Editorial Staff

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

  • Gretchen Louise

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

    • http://www.wpbeginner.com Editorial Staff

      Yes. Change line 2 code from:

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

      • Gretchen Louise

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

        • http://www.wpbeginner.com Editorial Staff

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

        • Gretchen Louise

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

        • http://www.wpbeginner.com Editorial Staff

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

      • Gretchen Louise

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

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

  • VIVROCKS

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

  • Collin Weeks

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

  • Mairaj Pirzada

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

  • Helder

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

    • http://www.wpbeginner.com Editorial Staff

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

  • Rafiul Islam

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

  • Lorenzo Solís

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

    Thanks!

    • http://www.wpbeginner.com Editorial Staff

      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.

      • Lorenzo Solís

        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.

        • http://www.wpbeginner.com Editorial Staff

          Check out the code now. It should work.

  • Paul

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

  • Lisa

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

    • http://www.wpbeginner.com Editorial Staff

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

  • Lisa

    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!

  • http://twitter.com/albertalbs Albert Albs

    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?

    • http://www.wpbeginner.com Editorial Staff

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

      Edit: Just updated the code :)

      • http://twitter.com/albertalbs Albert Albs

        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.

  • Gabe Diaz

    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!

  • http://twitter.com/skinnyemmie Emily Sandford

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