• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Display the author box only to non-login users in WordPress

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.

WordPress 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

First, create the author box using WordPress blocks.

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.

structure author box block WordPress
add CCS clases WordPress block editor

3. Modify the rendering of the block

Add the following code at the end of functions.php or in your functionality plugin:

To see this and another 919 code snippets of this website, login or subscribe here.

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

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() by !is_user_logged_in(). You can even apply it to another type of block, you would only have to modify the CSS class.

Conclusions

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

Author box Block Editor PHP WordPress

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2023

Lost your password?