Skip to content

Instantly share code, notes, and snippets.

@MrJSdev
Last active March 23, 2017 18:59
Show Gist options
  • Save MrJSdev/f98e59a492a816801aa54842ee793c13 to your computer and use it in GitHub Desktop.
Save MrJSdev/f98e59a492a816801aa54842ee793c13 to your computer and use it in GitHub Desktop.
Read More link when someone copy your article
<?php
/* Do Not copy open php tag */
/**
* @author Hunk Riyaz
* @link http://extracatchy.net
*/
function add_copyright_text() {
if (is_single()) { ?>
<script type='text/javascript'>
function addLink() {
var selection = window.getSelection();
var htmlDiv = document.createElement("div");
for (var i = 0; i < selection.rangeCount; ++i) {
htmlDiv.appendChild(selection.getRangeAt(i).cloneContents());
}
var selectionHTML = htmlDiv.innerHTML;
var pagelink = "<br/><br/>Read more at ExtraCatchy: <a href='"+document.location.href+"'>"+document.location.href+"</a>";
var copytext = selectionHTML + pagelink;
var newdiv = document.createElement('div');
newdiv.style.position = 'absolute';
newdiv.style.left = '-99999px';
document.body.appendChild(newdiv);
newdiv.innerHTML = copytext;
selection.selectAllChildren(newdiv);
window.setTimeout(function () { document.body.removeChild(newdiv); }, 0);
}
document.oncopy = addLink
</script>
<?php
}
}
add_action( 'wp_head', 'add_copyright_text');
@MrJSdev
Copy link
Author

MrJSdev commented Mar 23, 2017

going to make tutorials of this code at my website http://extracatchy.net

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment