Get a Link Back to Your WordPress Site from Copy & Pasted Text

Did you know that copying and pasting makes up 82% of all content sharing on the web — 4.5 times more than social sharing buttons?

That’s what advertising giant 33across/Tynt found in a study done at the end of last year.

You may find that interesting. But so what? How can you use that information for your own site? Read on to find out.

Get a Link Back from Copy & Pasted Text

Have you ever copied a piece of text from a website into an email, a file, a forum, or even onto your own site, and suddenly you notice that appended to the end of the text was a link back to the page where it came from?

Pretty cool, right? And when you think about it, pretty good for SEO purposes too. Well, that’s what this little piece of code below will let you achieve for your own site.

If you paste this in as is, it will show a link back to the page with the following style. Let’s say your post is titled “Little Orange Squares” and your site is mysite.com.

Read more: Little Orange Squares http://mysite.com/little-orange-squares/

You can change this to say something like “Read more at MySite” by just adding what you’d like at the end of the “Read more” text.

Paste the following into your functions.php file (Appearance > Editor > Theme Functions – functions.php).

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
26
27
28
29
30
31
32
33
34
35
36
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: <?php the_title(); ?> <a href='<?php echo get_permalink(get_the_ID()); ?>'><?php echo get_permalink(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');

If you are using a link shortening service, then you can use the code above with it, but replace get_permalink with wp_get_shortlink.

If you would like to make your own simple plugin for this code, you can learn how to do that here.

(Thanks to wpbeginner for this code.)

Photo: Chain Link Close Up from BigStock

Tags , ,

Comments (33)

      • The stats themselves don’t really surprise me, but I do question the effectiveness because it tends to be a subtle link. Subtle enough that I think it will rarely get clicked on, and click-throughs are the key since no-follow is now prevalent.

        • I think if it’s interesting, then people do look to read more. Also, there has been debate in the SEO community for years as to whether no-follow links can actually help you or not. Certainly they don’t carry the same weight as do-follow links, but some people swear that they can still help.

  1. You can use the “pretty permalink”… just change the call of wp_get_shortlink to get_permalink and it works. (Make sure you get both of them.)

    The original article used the shortlink because they were using a shortening service to filter it through.

    Something messed up in your own edit or C&P here, because the code from WPMU doesn’t work when thrown into functions, but when grabbing directly from WPBeginner, it does.

      • No problem. =)

        I think there was an issue with your CDATA wrapper (now that it’s gone and I’ve had a little more time to look into it with the benefit of my home pc, lol).

        I just tried popping my own CDATA wrapper on it and it still works, so maybe the typo was there?

  2. I must be messing something up, because I insert this into my functions.php file and I’m not seeing any links being included with my text when I paste it someplace else.

    I’ve cleared my browser caches, and purged pages from W3 Total Cache as well. What am I missing?

    • Charlie – I’m not sure what’s happening. It works for me. Did you try the code from wpbeginner itself? If that works, then it seems something may be affecting code copied here.

      • Nope, no luck with that either. I try copying the text in both custom-functions.php (which my themes come with for the purpose of custom functions rather than editing core files), and I also place it in the main functions.php file as well.

        Everything is being done FTP, not via WordPress editor.

        • Is it just not showing up in the source of your single post pages at all?

          If it is, the next thing to look at is make sure that your div class for “entry-content” is the same as what’s referenced in the code.

          I’m running under the assumption that you’re testing this on a live site and not a dev environment (based on your FTP comment). Are you connected to Cloudflare? If so, have you tried emptying the cache/pausing it?

          • I haven’t actually checked source code, but it has certainly been inserted in two live sites at the moment.

            http://hottipscentral.com
            http://charliepryor.net

            Both of these templates come with a custom functions.php file used for entering functions that aren’t included with the theme. I’ve actually set up advertising within content entirely with custom adsense functions using short codes.

            The code above is still inserted. I used a get_permalink for HotTips, and the shorter URL version for my personal site, since I’ve got http://cpry.net hooked to it.

            Have a look with copying text from those sites and see if it works for you. I’ve tried emptying cache in my browsers (multiple), and have also had W3 dump and purge on both sites.

            Something I’m thinking though, is that I have W3 Total Cache settings enabled for caching page, objects, and browser. I’m not sure why these settings would effect it, but I haven’t attempted shutting it off for those settings yet (because I don’t see a connection).

            To my knowledge, I’m not associated to CloudFlare in any way.

          • Charlie – Yeah, it’s not working for me on those sites. I’m guessing it probably does have something to do with your caching.

    • Meh… Cacheing totally disabled, and still nothing. Something has to be interfering here. I just have to track it down. Your code is no doubt solid, because clearly it works for others.

      It’s on my end. I’ll figure it out sooner or later. Good post Joe!

      • The good news – I am seeing it in source code of hottips and charliepryor. =)

        Bad news – your theme doesn’t nest divs. I’d change the ‘entry-content’ line to ‘post’ on hottips and see what happens. On charliepryor, try changing it to ‘entry-part’.

        That’s one of the frustrating things about java sometimes – so darn specific.

      • I ran into similar problems Charlie – and the issue was that the containing DIV for the post content did not have the ‘entry-content’ class specified in the javascript code posted here. I had to modify the JS so that it corresponded to the DIV containing the post content in my template. Hope that helps.

  3. Really usefull, many thanks Joe.

    Charlie, RJ Webb is right i think, your shoul verifiy the container of your content in single.php and change le line 8 of the code above : document.getElementsByClassName(‘entry-content’)[0], true)) { to replace entry-content by the name of container you find.

    Thomas

  4. For some reason the code is disabling my paragraphs when pasted into another source. I copied and pasted the existing code into a plugin in and all else seems to work on a clean local install.

    Anyone who knows code have an idea on what’s going on?
    Thanks!

  5. Has someone made a plugin of this yet to run on multisites? I tried, but due to lack of programming skills I get fun errors like Parse error: syntax error, unexpected T_STRING

    While it would take someone like you to fix in two minutes, it will take me hours to figure out what that means.

    Hope to see a plugin soon! :)

    G

  6. Hi,

    Many thanks for this great article. I do have a question, I need to know how can I remove the post title from the read more text?

    I hope someone can help me.

    Cheers

Participate