Find out how to get the author box to be displayed only to non-logged-in users in a WordPress theme.
Displaying the author box at the end of posts can be an interesting strategy if you want sporadic visitors to “remember your face”.
On the other hand, if you have loyal subscribers, you’d rather not bore them with your face every time they finish reading a tutorial, don’t you think?
Personally, I’m betting on this strategy in OsomCode. We already saw how to achieve it for a Genesis Framework theme.
In this case we are going to see how to achieve it using an author box created with blocks.
Let’s see how:
Steps to hide the author box from logged in users in WordPress
1. Create an author box
2. Add a custom CSS class
Once you have created the block, go to the “Advanced” section and add
author-box under “Additional CSS class(es)” of the “Columns” block.
3. Modify the rendering of the block
Add the following code at the end of functions.php or in your functionality plugin:
In this code you use the filter
pre_render_block, which allows you to access the attributes of all blocks.
The conditionals you use are:
is_user_logged_in: the user is logged in
$parsed_block['attrs']['className'] === 'author-box': the block CSS class is
That is, if the user is logged in and the block has the CSS class
author-box it will not display the author box. On the other hand, visitors (who are not logged in) will see the author box displayed.
However, keep in mind that since you are 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.
You can also use this same strategy to show the block only to logged in users, replacing
!is_user_logged_in(). You can even apply it to another type of block, you would only have to modify the CSS class.
Now you know how to create an author box with native WordPress blocks that displays only to non-logged-in users.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂