Hide/Show block if not/logged-in

There are many plugins that help to hide/show elements if a user is [not] logged in.

In this tutorial I’ll focus on hiding/showing individual paragraphs of a page/post in WordPress. The method works really well in the Gutenberg editor!

Process

  • Edit theme css
  • Design & create your page/post
  • Tag paragraphs to make them visible/hidden as needed

Edit theme

In WP Admin, locate the theme editor, and append some CSS to the main theme css file: E.g.

On the right, locate the CSS file – in my case, it’s the style.css file.

Append the following to the end of the file

… and click [Update file] to save changes.

Create Post/Page

Create a post or page with the content you need. For example, create a paragraph that is visible only to logged-in users, and another that’s visible only to guests.

>> This is visible to guests

>> This is visible to logged-in users

Tag your content

For each paragraph, in the Block Editor, select/expand Advanced and add an Additional CSS Class:

>> This is visible to guests

For guests, add class: guest-content

>> This is visible to logged-in users

Save your changes

Test your page/post

View the page when logged-in and as a guest – you should see the appropriate content as tagged above.

I hope you found this useful, please comment below and/or share by clicking the social buttons!

References

Qudos to Ismael at https://kriesi.at/support/topic/hiding-content-unless-user-is-logged-in/ for the original idea. I have adapted it to cover both logged-in and guest users.

Page rating (click to vote!)

Leave a comment

Share via
Copy link
Powered by Social Snap

On this website we use first or third-party tools that store small files (cookie) on your device. Cookies are normally used to allow the site to run properly (technical cookies), to generate navigation usage reports (statistics cookies) and to suitable advertise our services/products (profiling cookies). We can directly use technical cookies, but you have the right to choose whether or not to enable statistical and profiling cookies. Enabling these cookies, you help us to offer you a better experience.