Skip to content

Instantly share code, notes, and snippets.

Created September 1, 2016 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4d4f6c9308ae71aaeaa3c09a120bce1c to your computer and use it in GitHub Desktop.
Save anonymous/4d4f6c9308ae71aaeaa3c09a120bce1c to your computer and use it in GitHub Desktop.
<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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment