Last active
October 13, 2018 15:33
-
-
Save akshansh1998/5a03e06829e38860adc835342be15121 to your computer and use it in GitHub Desktop.
Add Link to Copied Text from Your Website
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 facttuts.com: <?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); | |
} | |
} | |
//Script By https://facttuts.com Do Not Edit or Change Other then Copyright Text and Site title | |
document.oncopy = addLink; | |
</script> | |
<?php | |
} | |
} | |
add_action( 'wp_head', 'add_copyright_text'); | |
//Don’t forget to change the copyright text, and add your own site’s title there. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment