Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimcoleman/14b038efb2e5ca12b8f9d40224176a8f to your computer and use it in GitHub Desktop.
Save kimcoleman/14b038efb2e5ca12b8f9d40224176a8f to your computer and use it in GitHub Desktop.
Hide widgets by widget area ID based on the user's membership level.
<?php
/**
* Hide widgets by widget area ID based on the user's membership level.
* Update the $hide_sidebars_array with the array of sidebar IDs you want to filter.
* Update the $show_for_levels_array with the array level IDs you want this sidebar visible for.
*/
function my_pmpro_widget_display_by_level_id_callback( $instance, $widget, $args ) {
// Set an array of widget areas by ID to filter.
$hide_sidebars_array = array( 'sidebar-1', 'sidebar-2' );
// Get the current queried object to check access.
$show_for_levels_array = array( 1, 2, 3 );
// Check if this widget area should be filtered.
if ( in_array( $args['id'], $hide_sidebars_array ) && function_exists( 'pmpro_hasMembershipLevel' ) && ! pmpro_hasMembershipLevel( $show_for_levels_array ) ) {
// Hide the widget if user doesn't have access to view the queried object.
return false;
}
return $instance;
}
add_filter( 'widget_display_callback','my_pmpro_widget_display_by_level_id_callback', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment