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

How to prevent code copying in WordPress articles

藏色散人
藏色散人forward
2019-11-08 11:34:152503browse

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: 0ba56bcead12a5a0498f3f901b55116aAdd 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>

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);

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; );
}

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!

Statement:
This article is reproduced at:zmingcx.com. If there is any infringement, please contact admin@php.cn delete