Aller au contenu
WordPress

Disable Right-Click on a WordPress Site (and What It Can’t Protect)

Hook a tiny script into wp_footer that cancels the contextmenu event. Put it in a child theme or a site-specific plugin, not the parent theme.

Testé sur WordPress 6.x

Disabling the right-click menu stops casual visitors from saving images or copying text with a click. It takes a few lines of code – but be clear about what it does and doesn’t do before you add it.

A deterrent, not protection

Anyone can still view the page source, use keyboard shortcuts, take a screenshot or switch JavaScript off. It also annoys legitimate visitors who use the menu to open links in a new tab. For images that matter, use watermarks or lower-resolution web versions.

Where to put the code

Don’t edit your theme’s functions.php directly – updates overwrite it, and many servers (including this one) disable the theme editor for security. Use a child theme or a tiny site-specific plugin:

Create this file
wp-content/plugins/disable-right-click/disable-right-click.php
disable-right-click.php
u003c?phpn/**n * Plugin Name: Disable Right-Clickn * Description: Blocks the context menu on the front end (not in wp-admin).n */nnadd_action( 'wp_footer', function () {n    if ( is_user_logged_in() ) {n        return; // Editors keep their right-click.n    }n    ?u003en    u003cscriptu003en    document.addEventListener( 'contextmenu', function ( e ) {n        if ( ! e.target.closest( 'input, textarea' ) ) {n            e.preventDefault();n        }n    } );n    u003c/scriptu003en    u003c?phpn} );
  1. Create the folder and file above (via SFTP or your host’s file manager).
  2. In WordPress go to Plugins and activate Disable Right-Click.
  3. Log out (or use a private window) and right-click the site to test.

The snippet skips logged-in users and still allows right-click inside form fields, so people can paste into your contact form.

Inder Kochar

Écrit par

Inder Kochar

Ingénieur systèmes et réseaux – Je rédige les solutions que j’applique chez mes clients, testées sur de vrais systèmes. Bloqué sur un problème similaire ?

Continuer la lecture

Posez une question ou partagez votre solution

Votre e-mail n’est pas publié. Les commentaires sont modérés – merci de rester dans le sujet.