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.
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>
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( 'copyright', get_template_directory_uri() . '/copyright.js', array(), false ); } if (! current_user_can('level_10') ) { add_action( 'wp_enqueue_scripts', 'copyrightpro_scripts' ); }
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!