• Hello David,
    hope you’re doing well.

    I want to disable right click on WordPress without any plugin. Because installing a plugin for everything can affect site security and also slow down the site speed. So please provide some code to disable right-click in WordPress.

    Thanks for your support!

    • This topic was modified 3 years, 12 months ago by Shivam Pandey.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi there,

    Add this PHP Snippet to your site:

    add_action('wp_footer', function() {
        ?>
        <script>
        document.addEventListener('contextmenu', event => event.preventDefault());
        </script>
        <?php
    } );

    Adding PHP:
    https://docs.generatepress.com/article/adding-php/

    More info here on the Javascript thats being used here:

    https://stackoverflow.com/a/737043

    Thread Starter Shivam Pandey

    (@wikihelp360)

    David, I have added this custom function to my site but this code is not working properly. Your code only disables right click, but not the Copy (CTRL + C) and Paste (CTRL + V).

    Please help me to fix this problem.

    You can add this CSS to your site to stop users from selecting content:

    html {
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        -o-user-select: none;
        user-select: none;
    }

    Please note, as much as we like helping our users none of this is related to the Theme. All of these answers are available from a simple google search.

    Thread Starter Shivam Pandey

    (@wikihelp360)

    First of all, Thank you, David, I greatly appreciate your help!

    The above PHP snippet and CSS are working like a charm! ??

    But David, I have already installed 20+ plugins and I have already told you. I don’t want other plugins and I want to remove plugins one by one When I find alternative options. To use this custom function, I need to install the Code Snippets plugin so I have made some changes.

    First, I create a new Hook Element. And add the script like this:

    <script>document.addEventListener('contextmenu', event => event.preventDefault());</script>

    and set the hook to wp_footer then set the Display Rules to “Entire Site”.

    After that, I add the below CSS code in Appearance > Customize > Additional CSS.

    html {
        -webkit-user-select: none;
        -khtml-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        -o-user-select: none;
        user-select: none;
    }

    Finally, I achieve my desire result. The right click and copy and paste are now disabled. now users can’t steal my content.

    David, I want to ask one last question. Have I followed the correct method to disable right click and copy and paste? because I get the desired result without installing the “Code Snippets” plugin.

    Yes, that method is correct.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to Disable Right Click in GeneratePress?’ is closed to new replies.