If you’re using widgets on your WordPress site and want to customize them for members and non-members, check out the recipes below. We’ll provide a way to filter an entire widget area for members-only, as well as a recipe to hide a specific widget instance based on the current user’s membership level.
Hide an Entire Widget Area
The following recipe allows you to define dynamic sidebars (by sidebar ID) to hide or show based on the membership requirements of the active post ID. Any widgets placed in the filtered widget areas will not show if the current user does not have access to the post ID.
Update line 8 with the sidebar IDs you would like to filter. To discover the sidebar ID, you can look in your theme files to locate where the dynamic_sidebar is displayed.
Hide a Specific Widget Instance
The following recipe allows you to hide specific widget instances (regardless of the widget area they are placed in) based on the current user’s membership level. This code recipe can be extended for several membership level checks based on your needs.
Update the $hide_widget_instances_array
with the array of widget instance IDs you want to filter. and the pmpro_hasMembershipLevel check for your membership level IDs.
To discover the widget instance’s ID, you can use your browser’s Web Inspect features. The ID will be in a format widgettype-NUM
, for example text-2
or recent-comments-1
.
Adding the Recipe to Your Website
You can add this recipe to your site by creating a custom plugin or using the Code Snippets plugin available for free in the WordPress repository. Read this companion article for step-by-step directions on either method.
Free Course: Membership Site Development—The Basics
Develop a deeper understanding of membership site development in this beginner-level course. Learn how to make your site work better, save yourself time and money, and improve your site's performance.