Conditionally show a block based on the current author with a dynamic class

tdrayson

A SnippetClub member was using the great Block Visibility plugin by Nick Diego, however they came across a scenario where they wanted to show or hide sponsored widgets in the sidebar of a blog post, based on the author of the post. The Block Visibility plugin doesn’t have any built in way to conditionally show or hide blocks based on the author.

The snippet below allows you to add the class author-USERNAME or author-ID to a block and it will conditionally show based on if the post author matches the username or user ID.

1. Add the code snippet

Add the code below to functions.php or code snippet plugin.

Members content only

Become a SnippetClub member to gain instant access to this tutorial plus tons more premium content.

Already a member? Log in below.

Login Form

👋🏻 Weekly Tutorial Digest

I send out a weekly newsletter with links to new tutorials written in the last week, you can subscribe below.

Newsletter

🔒I won't send you spam, I promise