Protect Your WordPress Content from Copying and Right-Click with functions.php and Custom Plugin Methods

Method 1: Using the functions.php Method to Protect Content

The functions.php method allows you to integrate content protection directly into your WordPress theme. This method is effective and lightweight since it doesn’t require installing additional plugins.

Step 1: Open the functions.php File

  1. Log in to your WordPress dashboard.
  2. Navigate to Appearance → Theme Editor.
  3. Locate the functions.php file from the list of theme files on the right-hand side.

Step 2: Add the Protection Code

Scroll to the bottom of the functions.php file and add the following code:

function wp_advanced_content_protection() {
?>
<style>
/* Disable interactions and make elements initially invisible */
html, body {
pointer-events: none; /* Prevent interaction on the entire page */
-webkit-user-select: none; /* Disable text selection on Safari */
-moz-user-select: none; /* Disable text selection on Firefox */
-ms-user-select: none; /* Disable text selection on IE/Edge */
user-select: none; /* Disable text selection on Chrome/Opera */
visibility: hidden; /* Initially hide elements */
}
body * {
pointer-events: auto; /* Allow interactions for elements under the body */
visibility: visible; /* Make body content visible */
}
</style>

<script type="text/javascript">
(function() {
// Disable right-click, text selection, and copy actions
function disableActions() {
document.addEventListener('contextmenu', function(e) {
e.preventDefault();
e.stopImmediatePropagation(); // Stop extension interference
}, true); // Capture phase

document.addEventListener('selectstart', function(e) {
e.preventDefault();
e.stopImmediatePropagation(); // Stop extension interference
}, true);

document.addEventListener('keydown', function(e) {
if ((e.ctrlKey || e.metaKey) && (e.keyCode === 67 || e.keyCode === 85)) { // "C" and "U"
e.preventDefault();
e.stopImmediatePropagation(); // Stop extension interference
}
}, true);

document.addEventListener('dragstart', function(e) {
e.preventDefault();
e.stopImmediatePropagation(); // Prevent dragging
}, true);

document.addEventListener('copy', function(e) {
e.preventDefault();
alert('Copying content is not allowed on this site!');
e.stopImmediatePropagation(); // Stop extension interference
}, true);
}

// Create a Shadow DOM to isolate protected content from extensions
function applyShadowDOMProtection() {
const shadowHost = document.createElement('div');
const shadowRoot = shadowHost.attachShadow({ mode: 'closed' });

const protectedContent = document.createElement('div');
protectedContent.innerHTML = 'This content is protected from copy and right-click.';

shadowRoot.appendChild(protectedContent);
document.body.appendChild(shadowHost);

shadowRoot.addEventListener('contextmenu', function(e) {
e.preventDefault(); // Disable right-click inside the Shadow DOM
});
}

// Monitor for changes and re-apply protection if necessary
function monitorDOMChanges() {
const observer = new MutationObserver(() => {
disableActions(); // Reapply protections if tampered with
});

observer.observe(document, {
childList: true,
subtree: true,
});
}

// Override event listener functions to prevent tampering
function protectEventListeners() {
const originalAddEventListener = EventTarget.prototype.addEventListener;
const originalRemoveEventListener = EventTarget.prototype.removeEventListener;

EventTarget.prototype.addEventListener = function(type, listener, options) {
if (['contextmenu', 'selectstart', 'keydown', 'dragstart', 'copy'].includes(type)) {
console.log(`Blocked attempt to add listener for ${type}`);
} else {
originalAddEventListener.call(this, type, listener, options);
}
};

EventTarget.prototype.removeEventListener = function(type, listener, options) {
if (['contextmenu', 'selectstart', 'keydown', 'dragstart', 'copy'].includes(type)) {
console.log(`Blocked attempt to remove listener for ${type}`);
} else {
originalRemoveEventListener.call(this, type, listener, options);
}
};
}

// Apply protections and continuously re-apply them every 50ms
disableActions(); // Initial protections
protectEventListeners(); // Protect event listeners
monitorDOMChanges(); // Monitor for DOM tampering
applyShadowDOMProtection(); // Shadow DOM protection

setInterval(disableActions, 50); // Reapply every 50ms
})();
</script>
<?php
}
add_action('wp_footer', 'wp_advanced_content_protection');

Step 3: Save Changes

After pasting the code, scroll down and click Update File to save the changes.


Method 2: Creating a Custom Plugin for Content Protection

Creating a custom plugin gives you a modular way to protect your website content without directly editing your theme files.

Step 1: Create the Plugin Files

  1. Go to your WordPress installation directory using FTP or your hosting control panel.
  2. Navigate to the /wp-content/plugins/ folder.
  3. Create a new folder called unique-content-protection.

Step 2: Create Plugin Files

Inside the unique-content-protection folder, create two files:

  1. unique-content-protection.php: This is the main PHP file for your plugin.
  2. protection-script.js: This file will contain the JavaScript for protecting your content.

Step 3: Add Code to unique-content-protection.php

Open the unique-content-protection.php file and paste the following code:

<?php
/*
Plugin Name: WP Advanced Content Protection
Description: Protects website content from being copied, right-clicked, or selected, with advanced features.
Version: 1.0
Author: Your Name
*/

function enqueue_advanced_protection_script() {
wp_enqueue_script('advanced-content-protection', plugin_dir_url(__FILE__) . 'protection-script.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'enqueue_advanced_protection_script');

Step 4: Add Code to protection-script.js

In the protection-script.js file, paste the following JavaScript code:

 (function() {
// Disable right-click, text selection, and copy actions
function disableActions() {
document.addEventListener('contextmenu', function(e) {
e.preventDefault();
e.stopImmediatePropagation(); // Stop extension interference
}, true); // Capture phase

document.addEventListener('selectstart', function(e) {
e.preventDefault();
e.stopImmediatePropagation(); // Stop extension interference
}, true);

document.addEventListener('keydown', function(e) {
if ((e.ctrlKey || e.metaKey) && (e.keyCode === 67 || e.keyCode === 85)) { // "C" and "U"
e.preventDefault();
e.stopImmediatePropagation(); // Stop extension interference
}
}, true);

document.addEventListener('dragstart', function(e) {
e.preventDefault();
e.stopImmediatePropagation(); // Prevent dragging
}, true);

document.addEventListener('copy', function(e) {
e.preventDefault();
alert('Copying content is not allowed on this site!');
e.stopImmediatePropagation(); // Stop extension interference
}, true);
}

// Create a Shadow DOM to isolate protected content from extensions
function applyShadowDOMProtection() {
const shadowHost = document.createElement('div');
const shadowRoot = shadowHost.attachShadow({ mode: 'closed' });

const protectedContent = document.createElement('div');
protectedContent.innerHTML = 'This content is protected from copy and right-click.';

shadowRoot.appendChild(protectedContent);
document.body.appendChild(shadowHost);

shadowRoot.addEventListener('contextmenu', function(e) {
e.preventDefault(); // Disable right-click inside the Shadow DOM
});
}

// Monitor for changes and re-apply protection if necessary
function monitorDOMChanges() {
const observer = new MutationObserver(() => {
disableActions(); // Reapply protections if tampered with
});

observer.observe(document, {
childList: true,
subtree: true,
});
}

// Override event listener functions to prevent tampering
function protectEventListeners() {
const originalAddEventListener = EventTarget.prototype.addEventListener;
const originalRemoveEventListener = EventTarget.prototype.removeEventListener;

EventTarget.prototype.addEventListener = function(type, listener, options) {
if (['contextmenu', 'selectstart', 'keydown', 'dragstart', 'copy'].includes(type)) {
console.log(`Blocked attempt to add listener for ${type}`);
} else {
originalAddEventListener.call(this, type, listener, options);
}
};

EventTarget.prototype.removeEventListener = function(type, listener, options) {
if (['contextmenu', 'selectstart', 'keydown', 'dragstart', 'copy'].includes(type)) {
console.log(`Blocked attempt to remove listener for ${type}`);
} else {
originalRemoveEventListener.call(this, type, listener, options);
}
};
}

// Apply protections and continuously re-apply them every 50ms
disableActions(); // Initial protections
protectEventListeners(); // Protect event listeners
monitorDOMChanges(); // Monitor for DOM tampering
applyShadowDOMProtection(); // Shadow DOM protection

setInterval(disableActions, 50); // Reapply every 50ms
})();

Step 5: Activate the Plugin

  1. Log in to your WordPress dashboard.
  2. Go to Plugins → Installed Plugins.
  3. Activate the Unique Content Protection Plugin.

Conclusion

Both methods—whether using functions.php or a custom plugin—offer comprehensive content protection. By following these steps, you will ensure that your website content cannot be copied, right-clicked, or selected. These techniques, combined with continuous monitoring, provide robust defense even against browser extensions that attempt to bypass your restrictions.

FAQ

1. How can I protect my WordPress website content from being copied?

To protect your WordPress content, you can use JavaScript or plugins to disable right-click, text selection, and copy/paste functionality. You can add code to your theme’s functions.php file or create a custom plugin to block copying and other user actions. WordPress plugins like Disable Right Click also offer built-in solutions.

2. What is the role of functions.php in WordPress content protection?

The functions.php file is a key theme file in WordPress that allows you to add custom code to modify or extend website behavior. By adding JavaScript and CSS to functions.php, you can disable right-click, block text selection, and prevent content copying on your WordPress site.

3. How do I prevent right-click and copy-paste on my WordPress site?

You can prevent right-click and copy-paste by adding JavaScript that blocks the contextmenu, selectstart, and copy events. You can do this either by editing your theme’s functions.php file or by creating a custom plugin to inject the protection code site-wide.

4. Can browser extensions bypass content protection on WordPress?

Some browser extensions can bypass basic content protection methods, but advanced techniques such as reapplying protections every 50ms, using Shadow DOM for sensitive areas, and overriding event listeners can reduce the chances of extensions interfering with your site’s protections.

5. What should I do if my WordPress content is copied by other sites?

If your content has been copied, you can file a DMCA takedown request to have the infringing content removed. You can also refer to Google’s duplicate content policies to ensure your original content is properly indexed and any copied content is flagged.

Resources

1. WordPress Developer Resources – Theme Handbook (functions.php)

https://developer.wordpress.org/themes/basics/theme-functions/
This page explains how the functions.php file works in WordPress themes and how it is used to add features and customize functionality.

2. WordPress Developer Resources – Plugin Handbook

https://developer.wordpress.org/plugins/intro/
Learn how to create a plugin and hook into WordPress functions to modify or extend website behavior.

3. Google Search Central – Preventing Content Copying and Data Protection

https://developers.google.com/publisher-tag/guides/content-security-policy

Google’s guide to protecting web content from being copied and securing it from unauthorized scraping or sharing.

4. WordPress Plugin Directory – Content Copy Protection & Preventing Right-Click

https://wordpress.org/plugins/disable-right-click-for-wp/
A plugin from the WordPress plugin directory that disables right-click and prevents content copying on WordPress websites.

5. Digital Millennium Copyright Act (DMCA) – Takedown Policy

https://www.dmca.com/FAQ/What-is-a-DMCA-Takedown
DMCA’s official page on how to file a takedown request when your content is copied or misused without your permission.

    
    
// Your code here
    

Mohd Ozair

OzairWebs is a free WordPress and SEO resource Web Development company. The main goal of this site is to provide quality tips, tricks, hacks, and Web Development Services that allow WordPress beginners to improve their sites.

Leave a Reply