0

I have added function code in my WordPress for copy text, what I need now is to limit the word count to my desired range for copied text, no matter how much long paragraphs user copies.

Here is the current code :

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 beforepagelink = "Before Text: "
    var pagelink = "<?php the_title(); ?> <br /><br /><a href='<?php echo wp_get_shortlink(get_the_ID()); ?>'><?php echo wp_get_shortlink(get_the_ID()); ?></a><br />Source: Jamkhed Times"; //Change this if you like
    var afterpagelink = "<br /><br /> after text";
    var copy_text = beforepagelink + selection + pagelink + afterpagelink;
    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 anyone can suggest me an update then it will make my day! Thanks!!

Pratik
  • 1
  • 1
  • This code is potentially open to a security issue. It's definitely open to failures because none of the data is escaped. If you're going to insert arbitrary data into a script block like this, you should use `json_encode()` to do it. That will handle all the quoting and escaping for you. In any case, for the context of your question, can you remove the PHP and leave just the relevant JavaScript that you've tried? – Brad Jan 01 '22 at 07:04
  • Please see the link below. Does this answer help you? https://stackoverflow.com/a/20732481/4854391 –  Jan 01 '22 at 07:23

0 Answers0