• Resolved oriver

    (@oriver)


    Hi,

    I’m searching for a way to allow right click – so users can download/copy an article’s content, but at the same time protect images that have been donated to us. Is there a way to somehow allow right-click, but just disable this on images (inc featured & in content)? Or to otherwise disable the ‘save image…’ option that appears with a right click.

    Is anyone aware of any code I could try in code snippets?

    Thank you for any help

    • This topic was modified 2 years, 9 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 2 replies - 1 through 2 (of 2 total)
  • It’s worth mentioning that you can’t stop a determined person from getting an image you’re displaying on your site. They can get it through the browser’s developer tools or take a screenshot. All you can do is discourage the behavior.

    That said, you can disallow right click on all site images by inserting this code into your functions file or using the Code Snippets plugin. (There are a few plugins that claim to disable right click, but none seemed to work when I tested them.)

    add_action('wp_footer', 'wpsf_disallow_right_click_images');
    function wpsf_disallow_right_click_images() {
    ?>
    <script>
    	const images = document.getElementsByTagName('img');
    	for(var i = 0; i < images.length; i++) {
    		images[i].addEventListener('contextmenu', event => event.preventDefault());
    	}
    </script>
    <?php
    }

    I found the script on GitHub.
    https://gist.github.com/uzzalhcse/a24465a9418580ee01efbc7f0e3e833e

    Thread Starter oriver

    (@oriver)

    That worked perfectly, thank you so much.

    I understand if someone is determined to they can still do a work around, but I wanted to at least make some gesture of respecting the creators who are lending us their images.

    Thank you again for your help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Code to stop ‘save image..’ on right click’ is closed to new replies.