Skip to content

Control Your Widgets with Code with Widget Logic

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!

Yay! 🎉 You made it to the end of the article!
David Hayes

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Albert Soto
March 27, 2018 12:15 pm

Hi, Is there any way to display/hide a widget based on zip code?

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!

1
0
Would love your thoughts, please comment.x

Most Searched Articles

Best JavaScript Libraries and Frameworks: Try These 14 in 2024

In this post, we look at the best JavaScript libraries and frameworks to try out this year. Why? Well, with JavaScript being available in every web browser, this makes it the most accessible programming language of ...

20 Best Free WordPress Themes for 2024 (Responsive, Mobile-Ready, Beautiful)

If you're looking for only the best free WordPress themes in the market for this year, then you're in the right place. We have more than enough such themes for you right ...

12 Best WordPress Hosting Providers of 2024 Compared and Tested

Looking for the best WordPress hosting that you can actually afford? We did the testing for you. Here are 10+ best hosts on the market ...

Handpicked Articles

How to Make a WordPress Website: Ultimate Guide for All Users – Beginners, Intermediate, Advanced

Many people wonder how to make a WordPress website. They’ve heard about WordPress, its incredible popularity, excellent features and designs, and now they want to join the pack and build a WordPress website of their own. So, where does one get ...

How to Start an Ecommerce Business: Ultimate Guide for 2024

Is this going to be the year you learn how to start an eCommerce business from scratch? You’re certainly in the right place! This guide will give you a roadmap to getting from 0 to a fully functional eCommerce business. ...