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.
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 Widget Options plugin:
- Go to Plugins in the sidebar
- Click the Add New button
- Enter widget options in the sidebar
- Click the Install Now button
- Click the Activate button
- 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.
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:
- Go to Appearance > Widgets in the WordPress sidebar
- Expand the widgets Sidebar section if it is not already expanded
- Scroll down to the bottom of the widget, below the content area
- Select the Devices tab
- Set Hide/Show to either hide or show the widget
- Use the checkboxes to choose which devices the widget appears on
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?
- Select the Visibility tab
- Select either Show or Hide on checked pages
- Select the Post Types tab
- Scroll down to the bottom and use the checkboxes to choose whether to show the widget on posts or pages.
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.
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.
- Go to Appearance > Widgets
- Expand the Sidebar section
- Expand the widget you want to control visibility
- Select the Other Settings tab
- Select the Logic tab
- Enter: is_user_logged_in()
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.