-1

I wanted to disable right click on my wordpress site. I have written a small snippet and inserted into body which disables right click, CTRL events and keyups.

<body oncontextmenu="return false" onselectstart="return false" onkeydown="if ((arguments[0] || window.event).ctrlKey) return false"></body>

but it is annoying when I want to copy something on site. is there a way I can modify the current snippet like only logged in users would be able to access the disabled events?

Cheers.

Roshan Zaid
  • 336
  • 1
  • 4
  • 21
  • check this please https://stackoverflow.com/questions/737022/how-do-i-disable-right-click-on-my-web-page – Full Stop Jun 28 '21 at 06:49
  • Well, I get it @RonvanderHeijden The point is stealing contents. I want to disable CTRL + Copy and right click. a person with a basic knowledge wouldn't know to view source with shortcuts. – Roshan Zaid Jun 28 '21 at 09:11
  • when the client requires it to be that way, you do it that way. :/ I know, as a SE, I get it. – Roshan Zaid Jun 28 '21 at 09:14
  • I am not clear of your point. what factors could affect UX in this case. – Roshan Zaid Jun 28 '21 at 09:19

4 Answers4

0

Why not using existing wordpress plugin?

for example using this

This plugin is used to disable right click on website to prevent cut, copy, paste, save image, view source, inspect element etc.

But when Administrator or Site Editor is logged in, he can access everything without any of the above restrictions.

0

You could simply put a conditional inside the body tag. You would want this in your theme's header.php file. You also want the wordpress function body_class to include all of the other classes that should appear in the body tag.

<body <?php if ( !is_user_logged_in() ) {
    echo 'oncontextmenu="return false" onselectstart="return false" onkeydown="if ((arguments[0] || window.event).ctrlKey) return false"';
} ?> <?php body_class(); ?>>
Howard E
  • 5,454
  • 3
  • 15
  • 24
  • Unfortunately Howard, it doesn't seem like working. Injected in header.php. and I am not cleat of include all of the other classes. – Roshan Zaid Jun 28 '21 at 08:33
  • You need to find where the body is declared in your theme. – Howard E Jun 28 '21 at 09:44
  • I injected through the elementor custom code. – Roshan Zaid Jun 28 '21 at 10:28
  • How did you do this? Explain it like I don't know what this elementir custom code thing is. Please add to your question if it's relevant. I would say that I also agree with the other comments that disabling the right click is pointless. – Howard E Jun 28 '21 at 12:21
  • disabling right click is pointless, I understood. for some reason, its required to add the snippet for disabling highlighting contents and copy paste or view its source. The snippet you shared, I inserted in elementor custom codes body start instead of my theme's body. My theme files are kinda complicated. – Roshan Zaid Jun 28 '21 at 12:31
  • The `body` tag is definitely somewhere in your theme (most likely in header.php). You can't just drop another `` tag into the page. – Howard E Jun 28 '21 at 13:51
0

Just disable context menu not select or other events.

<body <?php if(!is_user_logged_in()) { echo 'oncontextmenu="return false"'; }?>></body>
Vicky P
  • 553
  • 6
  • 12
  • Please read the question. The logged in users can be able to right click or copy contents when others shouldn't. – Roshan Zaid Jun 28 '21 at 08:23
0

First I have created a JS page in the directory named: "preventcopy.js" and added restricted the events using below JS

document.oncontextmenu = 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;
  }
};
if (parent.frames.length > 0) top.location.replace(document.location);

and then in function.php I enabled access for logged in users as below.

function copyrightpro_scripts() {
    wp_enqueue_script( 'copyrightpro', get_template_directory_uri() . '/copyprevent.js', array(), false );
}
if ( !is_user_logged_in() ) {
    add_action( 'wp_enqueue_scripts', 'copyrightpro_scripts' );
}

That's how it is been accomplished. But I would highly suggest enabling these features for users considering the user experience.

Roshan Zaid
  • 336
  • 1
  • 4
  • 21