Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Created December 13, 2018 17:11
Show Gist options
  • Save plugin-republic/d2a9b106168ed007e30b0e7c084e3156 to your computer and use it in GitHub Desktop.
Save plugin-republic/d2a9b106168ed007e30b0e7c084e3156 to your computer and use it in GitHub Desktop.
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