Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Last active August 1, 2023 12:42
Show Gist options
  • Save kimcoleman/227edd825acbfd7aaaf5ba334c8c1482 to your computer and use it in GitHub Desktop.
Save kimcoleman/227edd825acbfd7aaaf5ba334c8c1482 to your computer and use it in GitHub Desktop.
Hide widgets by widget instance ID and membership level to protect specific widgets for members only.
<?php
/**
* Hide widgets by widget instance ID for protected members only content.
* Update the $hide_widget_instances_array with the array of widget instance IDs you want to filter.
* Update the pmpro_hasMembershipLevel check for your membership level IDs.
*
* Add this code below to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_widgets_by_membership_level( $instance, $widget, $args ) {
// Set an array of widget areas by ID to filter.
$hide_widget_instances_array = array( 'text-3', 'recent-comments-1' );
// Check if this widget instance should be filtered.
if ( in_array($widget->id, $hide_widget_instances_array ) && ! pmpro_hasMembershipLevel( 1 ) ) {
return false;
}
return $instance;
}
add_filter('widget_display_callback','my_pmpro_widgets_by_membership_level', 10, 3);
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Hide Widgets for Members Only" at Paid Memberships Pro here: https://www.paidmembershipspro.com/hide-widgets-members/

@andrewlimaza
Copy link

To extend on the level ID's (to support multiple level ID's) please see here - https://www.paidmembershipspro.com/documentation/content-controls/require-membership-function/

@produktivweb
Copy link

How to modify the code, please?
I have four membership levels and four widgets.
I would like to display each of the widgets for the different levels of membership.
Any help would be greatly appreciated, thank you!

@andrewlimaza
Copy link

@produktivweb please open a support ticket on www.paidmembershipspro.com/support for help with this as it would require a bit more work and explanation than we can provide in the comment section here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment