Instantly share code, notes, and snippets.

Embed
What would you like to do?
Programmatically prevent a widget from being rendered
<?php
/**
* This will hide widgets
* @param array $instance The current widget instance's settings.
* @param WP_Widget $this The current widget instance.
* @param array $args An array of default widget arguments.
*/
function wcmo_filter_widget_display_callback( $settings, $widget, $args ) {
$can_access = false; // You need to set your conditions here
if( $can_access || in_array( $widget->name, $whitelist ) ) {
// If the user can view the widget, just return the settings
return $settings;
}
// Otherwise, return false to prevent the widget from rendering
return false;
}
add_filter( 'widget_display_callback', 'wcmo_filter_widget_display_callback', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment