Using Copy and Paste for SEO Purposes with WordPress

copyandpaste

        As you’re browsing the web, what’s one of the main things that you’re always doing? You’re always copy and pasting. Links, pictures, emails; you name it, you’ve copy and pasted it. Being an owner or employee of a WordPress site, you’d like that when people copy and paste content from your site, you get some credit for your work and maybe even a little bit of automatic advertising. You’ve seen it done. You sometimes copy and paste something and something else shows up with it; a little tidbit of text with words like “Read more at:” and then a link back to where you got the content. Well now you too can use this incredibly useful function with WordPress.

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');

Editorial Staff, wpbeginner

For PasteBin version of code above, see http://pastebin.com/HSr4tYJ4.

        All you have to do is take that code and paste it into Appearance > Editor > Theme Functions – functions.php in WordPress, and it’s up and running on your site. If you’re using a link shortening service such as tinyurl.com, change get_permalink to wp_get_shortlink.

        Well it’s all nice and everything that you can do that, but now you want to make another website and you really want to use this awesome tool. You go to your old site and begin the tedious job of searching through your functions.php file for the right bit of code. Why go through all that stress and time when you can just make a plugin in the first place that can be used with any theme or site?

Starting

        The first thing to do is fire up a text editor. Notepad or TextEdit will be fine. Open up with a PHP tag,

/*
Plugin Name: Copy and Paste Add-on
Plugin URI: N/A
Description: Adds on text to any copy and paste which backlinks to the specified site.
Original Code for Plugin Template: http://wpmu.org/how-to-create-your-own-super-simple-wordpress-plugins/
Original Code for Copy and Paste: http://www.wpbeginner.com/wp-tutorials/how-to-add-a-read-more-link-to-copied-text-in-wordpress/ 
Version: 1.0
Author: Tzvi Joffre
Author URI: N/A
License: GPL2
*/

Joe Foley, WPMU.org

For PasteBin version of code above, see http://pastebin.com/g6Tuv9RP.

The Main Part

        Just take the code and paste it right below the header. You don’t need to worry about the PHP closing tag. In this case, it should look something like this:

<!--?php
/*
Plugin Name: Copy and Paste Add-on
Plugin URI: N/A
Description: Adds on text to any copy and paste which backlinks to the specified site.
Original Code for Plugin Template: http://wpmu.org/how-to-create-your-own-super-simple-wordpress-plugins/
Original Code for Copy and Paste: http://www.wpbeginner.com/wp-tutorials/how-to-add-a-read-more-link-to-copied-text-in-wordpress/ 
Version: 1.0
Author: Tzvi Joffre
Author URI: N/A
License: GPL2
*/
if (is_single()) { ?>// <![CDATA[
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 = "

 Read more:  <a href=''>"; //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;
// ]]>

For PasteBin version of code above, see http://pastebin.com/kQcFmS9f.

Saving your File

        Make sure when you’re saving your file to put in the .php extension. If you name it copypasteadd, you need to save it as copypasteadd.php. If your text editor gets fussy, write it in parentheses “copypasteaddd.php.”

Finishing Up

        You’re going to want to use your plugin, so now let’s go over how to install it. You’re going to need an FTP program such as FileZilla and know how to use it. All you have to do is upload the plugin file to wp-content/plugins in your WordPress directory. Then go to Plugins screen and find the plugin in the list and click Activate Plugin. Once you’ve done that, you’re finished. Congratulations, you’ve now gone a step further in spreading the word about your site.

 

  • korikisulda(コリキ)

    Useful? It’s annoying.