Home > CMS Tutorial > WordPress > How to prevent code copying in WordPress articles

How to prevent code copying in WordPress articles

藏色散人
Release: 2019-11-08 11:34:15
forward
2504 people have browsed it

The following JS code can effectively prevent others from directly copying your article. When using the frame tag to reference your article, it will automatically jump to the normal link of the article and disable the right-click menu. Below, the WordPress Tutorial column will introduce you to the specific method.

How to prevent code copying in WordPress articles

Usage method one:

Open the current theme header template header.php and find: Add the following code to the end:

<script>
// 禁止右键
document.oncontextmenu = function() {
return false
};
// 禁止图片拖放
document.ondragstart = function() {
return false
};
// 禁止选择文本
document.onselectstart = function() {
if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password") return false;
else return true;
};
if (window.sidebar) {
document.onmousedown = function(e) {
var obj = e.target;
if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD") return true;
else return false;
}
};
// 禁止frame标签引用
if (parent.frames.length > 0) top.location.replace(document.location);
</script>
Copy after login

Usage method two:

The above method is a bit confusing when viewing the source code. You can use the current theme Create a new file named copyright.js in the directory and add the following code:

// 禁止右键
document.oncontextmenu = function() {
return false
};
// 禁止图片拖放
document.ondragstart = function() {
return false
};
// 禁止选择文本
document.onselectstart = function() {
if (event.srcElement.type != "text" && event.srcElement.type != "textarea" && event.srcElement.type != "password") return false;
else return true;
};
if (window.sidebar) {
document.onmousedown = function(e) {
var obj = e.target;
if (obj.tagName.toUpperCase() == "INPUT" || obj.tagName.toUpperCase() == "TEXTAREA" || obj.tagName.toUpperCase() == "PASSWORD") return true;
else return false;
}
};
// 禁止frame标签引用
if (parent.frames.length > 0) top.location.replace(document.location);
Copy after login

Then add the following code to the end of the current theme function template functions.php:

function copyrightpro_scripts() {
wp_enqueue_script( &#39;copyright&#39;, get_template_directory_uri() . &#39;/copyright.js&#39;, array(),  false );
}
 
if (! current_user_can(&#39;level_10&#39;) ) {
add_action( &#39;wp_enqueue_scripts&#39;, &#39;copyrightpro_scripts&#39; );
}
Copy after login

Add to the code To judge, the administrator logs in and the anti-copying code is invalid.

Of course, the above method is just for fooling novices. After JavaScript is disabled in the browser, it will lose its effect.

The above is the detailed content of How to prevent code copying in WordPress articles. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:zmingcx.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template