Control Your Widgets with Code with Widget Logic
Widgets have been a core feature of WordPress forever, but by default there’s no easy way to turn a specific widget “off” on a particular page on your site. If, for example, you’re finding yourself stuck with a contact form in the sidebar of your Contact page, then you’ll know what makes Widget Logic (and the other solutions like it) a good idea.
However, there’s one big thing you need to know about how to use Widget Logic to create conditional widgets: the plugin has a fairly advanced user interface, one that requires you to actually think in PHP (pseudo)code.
To Use the Widget Logic Plugin, You Need to Understand WordPress’s Conditional Tags
Widget Logic does something unusual for such a popular (>300K installs) plugin: it requires you to know basic PHP syntax, and specifically the syntax for WordPress’s conditional tags.
So if little code fragments like
! is_singular( 'product' ) look familiar to you, you’ll fit right in with Widget Logic’s user interface, because you writing those conditional tags is what Widget Logic uses as its system for determining when to conditionally show and hide your widgets.
If you don’t know conditional tag syntax, we have some suggestions:
What to Do if You Don’t Know WordPress Conditional Tag Syntax
Not to worry! Here are a few options:
- Learn how to write WordPress conditional tags. They’re one of the most elegant parts of WordPress’s entire codebase, so learning how to think in conditional tags is a great place to start a coding career within WordPress.
- Learn how to do WordPress development generally! If learning this bit of PHP pseudocode gives you the WordPress development bug, you might enjoy our full course on the development essentials of WordPress, which covers conditional tags and all other key WordPress development topics in depth.
- Use a more interface-driven plugin that doesn’t require you to write PHP pseudocode. You can start by trying the similar Jetpack feature Widget Visibility, which we’ve also covered in a Quick Guide.
How to Use the Widget Logic Plugin, with Code Examples
Here’s our video guide to using Widget Logic:
And here’s a text summary of that information:
Using Conditional Tags with Widget Logic
- Install and activate the Widget Logic plugin.
- Go to your widgets, probably in the Customizer.
- Select the widget you’d like to control, expand it. You’ll notice a new “Widget logic:” section at the bottom. That box is where you write your code. It’s basically a place for you to make use of WordPress’s Conditional Tags.
Actually using those conditional tags properly is the tricky part. Here are a few usage examples:
- To show a widget only on your site’s static homepage, you’d write
- To show a widget everywhere its widget area is invoked, except on your posts of type Page (such as your About page), you’d write
! is_page()(meaning the widget should show up “if ‘is page’ is not true”).
- To show a widget only on single posts of type Course, you’d write
is_singular( 'course' ).
Widget Logic is a very helpful plugin, but a surprisingly complex one to use. If you’d like to learn more about WordPress’s widget system in general, or about its conditional tags, here are further resources:
Thanks for reading!