Control Your Widgets with Code with Widget Logic

screenshot of the Widget Logic plugin

This Quick Guide covers how to use the Widget Logic plugin, the popular plugin to help you show and hide WordPress widgets on specific pages and posts.

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_page() or ! 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:

  1. 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.
  2. 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.
  3. 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

  1. Install and activate the Widget Logic plugin.
  2. Go to your widgets, probably in the Customizer.
  3. 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 is_front_page().
  • 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' ).

Further Reading

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:

How to Use WordPress Conditional Tags

Creating WordPress Widgets: The Complete Guide

Thanks for reading!


1 Response

Comments

Add a Comment

Your email address will not be published. Required fields are marked *