HOW TO PROTECT YOUR SITE CONTENTS FROM BEING COPIED IN WORDPRESS

Learn how to protect your WordPress site contents from being copied using a simple code snippet. This tutorial walks you through adding custom functions to disable right‑click, text selection, and content theft. Safeguard your valuable posts and pages with practical coding tips to enhance your website’s security.

Here’s a tested snippet you can paste directly into Code Snippets (set it to run on the frontend only):

Code Snippet Used:

function riches_block_copy_rightclick() {
    // Only run for non-admins
    if ( current_user_can('administrator') ) {
        return;
    }
    ?>
    <script type="text/javascript">
    document.addEventListener('DOMContentLoaded', function() {
        // Utility: show popup
        function showWarning() {
            // Prevent stacking
            if (document.querySelector('.riches-copy-blocker')) return;
            var el = document.createElement('div');
            el.className = 'riches-copy-blocker';
            el.textContent = "Please you are not allowed to do such, contents on this site has Copyright Protection, be Warned!";
            el.style.position = 'fixed';
            el.style.top = '20px';
            el.style.left = '50%';
            el.style.transform = 'translateX(-50%)';
            el.style.backgroundColor = '#c62828';
            el.style.color = '#fff';
            el.style.padding = '12px 16px';
            el.style.borderRadius = '6px';
            el.style.fontSize = '15px';
            el.style.fontWeight = '600';
            el.style.zIndex = '999999';
            el.style.textAlign = 'center';
            document.body.appendChild(el);
            setTimeout(function(){ if(el.parentNode){ el.parentNode.removeChild(el);} }, 5000);
        }

        // Block right-click
        document.addEventListener('contextmenu', function(e){
            e.preventDefault();
            showWarning();
        });

        // Block copy
        document.addEventListener('copy', function(e){
            e.preventDefault();
            showWarning();
        });

        // Block text selection
        document.addEventListener('selectstart', function(e){
            e.preventDefault();
            showWarning();
        });

        // Block common keyboard shortcuts
        document.addEventListener('keydown', function(e){
            var k = (e.key || '').toLowerCase();
            var ctrl = e.ctrlKey || e.metaKey;
            if (
                (ctrl && (k === 'c' || k === 'x' || k === 's' || k === 'p' || k === 'u')) ||
                (ctrl && e.shiftKey && (k === 'i' || k === 'j' || k === 'c')) ||
                (k === 'f12')
            ) {
                e.preventDefault();
                showWarning();
            }
        });
    });
    </script>
    <style>
    body {
        -webkit-user-select: none !important;
        -moz-user-select: none !important;
        -ms-user-select: none !important;
        user-select: none !important;
    }
    </style>
    <?php
}
add_action('wp_footer', 'riches_block_copy_rightclick');
Share Post:

Join My Newsletter

Get expert tips, handy tools, and helpful tutorials delivered straight to your inbox absolutely free.

NewsLetter Form

Your privacy is important to me. Rest assured, your email address will remain completely confidential and will not be shared with any third party