Show or hide widgets on some pages or if not logged in WordPress

If you have WordPress site sometimes you want to show a widget only if a user is logged in or not logged in, or on a specific page. This is achieved with a widget visibility plugin.

The sidebar is a great place to show related links, adverts, images, your personal bio, an email signup form, special offers and more. However, you sometimes don’t want them to show on every page on your website.

Only show WordPress widgets on certain pages or to certain people. Control widget visibility.

For example, if you have a Privacy page it would not be appropriate to display an advert in the sidebar. You should hide widgets on pages like that, Terms of Service, and similar pages. Your theme may provide a plain page without a sidebar, but if not, you can hide widgets on pages, but not posts.

If you have a membership site or you allow users to log in, you might want to show a widget if users are logged in, such as links to their profile or members home page, latest members news, special offers or content exclusive to members, and so on.

You might also want to hide a widget if users are logged in and a bonus for members could be reduced adverts. A widget encouraging signups is pointless if people have signed up and logged in and it could be hidden.

Are all your widgets appropriate for mobile phones? You might want to hide a WordPress widget on mobile. What looks fine on a desktop computer with a big screen does not always look good on a small screen. All the sidebar items are displayed under a post and sometimes seem to go on forever when you are swiping up on a phone.

Some widgets could be hidden to reduce that long list of items and also to make mobile pages faster by limiting the content.

Install and configure Widget Options

The Widget Options WordPress plugin provides the features we need to control if and when widgets are displayed on a website. It has 60,000 active installations and an almost perfect score, which always inspires confidence in a plugin.

Install the Widget Options plugin in WordPress

Install Widget Options plugin:

  1. Go to Plugins in the sidebar
  2. Click the Add New button
  3. Enter widget options in the sidebar
  4. Click the Install Now button
  5. Click the Activate button
  6. Go to Settings in the sidebar and click Widget Options

This plugin has a large number of features which can make it very confusing when you first use it, however, turning off some of the features you don’t need right now helps to simplify it. They can always be enabled later.

Enable or disable components in the Widget Options WordPress plugin

The only features you need to control widget visibility are:

  • Pages Visibility
  • Devices Restriction
  • Display Logic

Disable all the other features for now, you can explore them some other time. Let’s keep it simple.

Hide widgets on mobile

To hide a widget on mobile, tablet or desktop:

  1. Go to Appearance > Widgets in the WordPress sidebar
  2. Expand the widgets Sidebar section if it is not already expanded
  3. Scroll down to the bottom of the widget, below the content area
  4. Select the Devices tab
  5. Set Hide/Show to either hide or show the widget
  6. Use the checkboxes to choose which devices the widget appears on

Show or hide WordPress widgets with the Widget Options plugin

I have only three modules enabled in the Widget Options plugin so there are only three tabs. If you have more modules enabled you will see more tabs.

In the screenshot above I am hiding an advert in an HTML widget on mobile phones, but showing it on desktop computers and tablets.

Hide a widget on a specific page

How do you hide a widget on a specific page or show or hide a widget on some pages but not others?

  1. Select the Visibility tab
  2. Select either Show or Hide on checked pages
  3. Select the Post Types tab
  4. Scroll down to the bottom and use the checkboxes to choose whether to show the widget on posts or pages.

Show or hide widgets with the WordPress Widget Options plugin

The controls for selecting whether to show or hide widgets are very broad. There are checkboxes to select individual pages because the number of pages on a site is small (About, Privacy, Terms, and so on) but you cannot select individual posts.

Only show widgets on specific categories of WordPress posts with Widget Options

However, the Taxonomies tab next to Post Types enables you to choose which post category shows/hides the widget. If you wanted to show or hide a widget on a specific post you would have to create a category for it and assign that post to it, then select the category on the Taxonomies tab.

You shouldn’t really create categories for single posts, but if you did, you could then show or hide the widget on that post.

Show or hide widgets when logged in

If you have a membership website, if you allow people to create user accounts, or if you create accounts for users like VAs, contributors and so on, you might want to hide a widget if not logged in or show a widget if logged in.

You can have one set of widgets in the sidebar for the public and another set of widgets for people that are logged in. You could provide useful links or extras to logged in people.

  1. Go to Appearance > Widgets
  2. Expand the Sidebar section
  3. Expand the widget you want to control visibility
  4. Select the Other Settings tab
  5. Select the Logic tab
  6. Enter: is_user_logged_in()

Show or hide widgets when users are not logged in with Widget Options WordPress plugin

This uses a feature called WordPress conditional tags and there are many of them. They basically ask whether something is True or False, or Yes/No if you like, and they can tell you all sorts of useful information. Here are just a few simple examples:

  • is_user_logged_in()
  • is_sticky()
  • is_page()
  • is_front_page()
  • comments_open()
  • is_category( ‘xyz’ )

In the screenshot you can see I used is_user_logged_in() but what if I wanted to show something if the user is not logged in? Just put a ! In front like this

! is_user_logged_in()

The ! changes True to False and False to True, so the line above means, is not true (is user logged in?). The answer must be True for the widget to display, so if the user is not logged in, the widget is hidden. I use it to hide some adverts for logged in users.

Use widgets elsewhere

It depends on your theme, but some use widgets in places other than the sidebar. Some themes have a widget area above and below posts, the home page may be composed of widgets, and there are always footer widgets.

The same widget visibility options are available in these areas, not just the sidebar, so a different home page, different footers, and different content before and after posts can be shown or hidden on certain devices or to logged in/out users, in specific categories and so on.

Alternatives to show or hide widgets

There are alternatives to the Widget Options plugin and Jetpack offers some very simple controls for showing or hiding widgets. It is easier to use than Widget Options but it does not have as many features.

Some people like Jetpack but some don’t, but if you have it installed on your site here is how to Show or hide WordPress widgets on specific pages.

For more information on Jetpack and why you may or may not want it, read Pros, cons and secrets of Jetpack plugin for WordPress.