Discover in this tutorial how to hide the content of any block to non-logged in users in WordPress, using code.
Depending on the website you have and your strategy, there are times when you may want to display different content depending on whether the user is logged in or not.
In today’s tutorial you will see how to control that the content of a block is only shown to logged in users, combining the filter pre_render_block
with the conditional tag is_user_logged_in
.
Let’s see how:
Steps to hide the content of a block from not-logged in users
1. Choose the block you want to hide
First, choose the block whose content you want to be visible after login.
If you want this to apply it to more than one set of blocks, you can choose the group block as the container.
2. Add a custom CSS class
Once you have created the block, go to the “Advanced” section and add hide-not-logged-in
under block’s “Additional CSS class(es)”.

3. Modify the block content access
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 997 code snippets of this website, login or subscribe here.
In this code you use the filter pre_render_block
, which allows you to access all blocks attributes, together with the conditional tag is_user_logged_in
.
In it you indicate that if the users are not logged in and the block has the CSS class hide-not-logged-in
, they will not be able to see the block content. On the other hand, active subscribers will be shown the complete block, just as you created it.
In the example you give the order to display an empty message so that the users without login do not see anything. But if you prefer to show a text indicating that, to see the content, they must login on their accout, you can modify it as follows:
$message ='To see the content you must login';
However, keep in mind that when using a PHP filter you have the limitation that you will only see the result in the web frontend, in the backend the changes will not be reflected.
Conclusions
Now you know how to make any block visible only to users who are logged into the web.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉