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>

7
  • 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: stackoverflow.com/questions/15500346/… Commented Aug 6, 2021 at 13:01
  • Use the WordPress is_admin function. But what is generating that .element though? A plugin, the theme, etc? Commented Aug 6, 2021 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. Commented Aug 6, 2021 at 13:04
  • Does this answer your question? How to hide content for logged out users? Commented Aug 6, 2021 at 13:08
  • 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. Commented Aug 6, 2021 at 13:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.