Forked from strangerstudios/my_pmpro_widget_display_callback.php
Created
August 22, 2018 14:55
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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