🛡️ How to Disable Right-Click and Text Selection in WordPress

If you’re running a WordPress website and want to protect your content from being copied, one effective method is disabling right-click and text selection on your pages. But what if you’re an admin and still want full access for testing or editing? In this tutorial, we’ll show you how to disable right-click and text selection for non-admin users only, using a simple script placed in your WordPress header.php file.

✅ Why Disable Right-Click and Text Selection?

While it’s true that no method can fully prevent content theft, disabling right-click and text selection adds an extra layer of protection against casual users trying to copy your text or images. This is especially useful for:

  • Blog owners who publish original content
  • Photography and portfolio sites

  • eCommerce stores with unique product descriptions

  • Educational platforms with protected course content

🔐 Restricting Only Non-Admin Users

It’s important that site admins and editors maintain full control and functionality while working on the site. The following code snippet ensures that the disable-right-click feature doesn’t affect logged-in admins:

💻 Code to Add in header.php


<?php if (!current_user_can('manage_options')) : ?>
<script>
function disableselect(e) {
return false;
}
function reEnable() {
return true;
}
document.onselectstart = new Function("return false");
if (window.sidebar) {
document.onmousedown = disableselect;
document.onclick = reEnable;
}
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
function mischandler(){
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
</script>
<?php endif; ?>

🧠 How It Works

  • current_user_can('manage_options') ensures the script does not run for admins.

  • JavaScript disables:

    • Right-click context menu

    • Mouse selection

    • Certain keyboard shortcuts

📝 Final Thoughts

Disabling right-click and text selection is a useful deterrent, especially if you publish unique, high-quality content. This simple tweak improves content protection without affecting your workflow as an admin.

Need help implementing this on your site? Drop a comment below or contact us for WordPress customization services!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Need Help ? Call Our award-winning support team 24/7 at 01-4983900

© 2023 All right reserved to sobiztrend.com