Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimcoleman/8d8d87e537c8b870e5a09729237c7ff4 to your computer and use it in GitHub Desktop.
Save kimcoleman/8d8d87e537c8b870e5a09729237c7ff4 to your computer and use it in GitHub Desktop.
Hide widgets by sidebar ID on members only content when the user does not have access.
<?php
/**
* Hide widgets by widget area ID for protected members only contnet.
* Update the $hide_sidebars_array with the array of sidebar IDs you want to filter.
* Add this code to your active theme's functions.php or a custom plugin.
*/
function my_pmpro_widget_display_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.
$queried_object = get_queried_object();
// Check if this widget area should be filtered.
if( in_array($args['id'], $hide_sidebars_array ) && ! empty( $queried_object ) ) {
// Hide the widget if user doesn't have access to view the queried object.
if ( function_exists('pmpro_has_membership_access') && ! pmpro_has_membership_access( $queried_object->ID ) ) {
return false;
}
}
return $instance;
}
add_filter('widget_display_callback','my_pmpro_widget_display_callback', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment