0

I want to completely hide the element if the user did not log in to my site on WordPress.

There is an identified class called .logged-in on WordPress, which is only for logged-in users. I can hide it by CSS, which is something like :not(.logged-in) .element{display: none;}, but this is unsafe because people still can inspect the element and set it to visible or view the page source.

Is there a way to really hide the element or remove it completely by a class name ( because I need to hide several elements with the same class name ) if the user did not log in by PHP?

.container {
  height: 100vh;
  width: 100%;
  background-color: #ddd;
  display: flex;
  align-items: center;
  justify-content: center;
}

.element {
  font-size: 60px;
  background-color: blue;
  color: white;
  padding: 15px 30px;
}
<div class="container">
  <p class="element">Hide me please</p>
</div>
  • 3
    javascript can also be turned off. Do it on a server with PHP then. Google: "hide content if user not loged in wodpress" Also duplicate: https://stackoverflow.com/questions/15500346/how-to-hide-specific-menus-in-wordpress-for-users-not-logged-in – ikiK Aug 06 '21 at 13:01
  • Use the WordPress is_admin function. But what is generating that `.element` though? A plugin, the theme, etc? – AndrewL64 Aug 06 '21 at 13:02
  • @AndrewL64 The element is just an example, they are not generated by any plugin or theme. Instead, I make them. I just don't want people who did not log in to see them. – Nojus Piper Aug 06 '21 at 13:04
  • Does this answer your question? [How to hide content for logged out users?](https://stackoverflow.com/questions/60728347/how-to-hide-content-for-logged-out-users) – Chris Haas Aug 06 '21 at 13:08
  • @ChrisHaas This is great but not the ideal result since it's putting the element inside the function instead of targeting it by a class name. All the elements are not necessarily in the same section. – Nojus Piper Aug 06 '21 at 13:11
  • Please share more details. Why is this question tagged with PHP, while it does not contain a single line of PHP? – Nico Haase Aug 06 '21 at 13:12
  • 1
    PHP is not "aware" of class names, nor HTML or JavaScript, it just spits out text that happens to look like that. If you want it to be aware, then you'd have to buffer it and run it through an HTML-aware parser such as `DOMDocument`. If security is your concern, then the text should be outputted in a non-secure context. As you pointed out, if you just hide things in CSS, that's not secure. JS would effectively do the same thing, too. I'm willing to flag this for re-open if you can elaborate further. – Chris Haas Aug 06 '21 at 13:23

2 Answers2

1

You need to wrap the code in an if statement, something like this:

<div class="container">
  <?php if (is_user_logged_in()): ?>
    <p class="element">I will only be visible to logged-in users.</p>
  <?php endif; ?>
</div>

Read the docs here.

CountKyle
  • 459
  • 3
  • 15
  • It seem like PHP can't target the element by a class name instead of wrapping the element into the function? My elements are not all in the same section and pages. I'll have to add this one by one which is time-consuming. – Nojus Piper Aug 06 '21 at 13:13
0

If you can't add a php condition where the actual div is generated then I can only think of two options:

  1. Javascript which can be bypassed - so that's not reliable
  2. PHP output buffering which would allow you to edit the html content after it has been generated. take a look at this. It might help.