Elementorで権限毎にwidgetを非表示にする方法
// GET CURRENT USER ROLE
function get_current_user_role() {
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
return $role[0];
} else {
return false;
}
}
// Remove the Elementor Widgets from the left-hand Panel in the Live Editor
global $elementor_widget_blacklist;
$elementor_widget_blacklist = [
// FREE
‘read-more’,
‘sidebar’,
‘audio’,
// PRO
‘posts’,
‘portfolio’,
‘slides’,
// ‘form’,
‘login’,
‘media-carousel’,
‘testimonial-carousel’,
‘nav-menu’,
‘pricing’,
‘facebook-comment’,
‘nav-menu’,
‘animated-headline’,
‘price-list’,
‘price-table’,
‘facebook-button’,
‘facebook-comments’,
‘facebook-embed’,
‘facebook-page’,
‘add-to-cart’,
‘categories’,
‘elements’,
‘products’,
‘flip-box’,
‘carousel’,
‘countdown’,
‘share-buttons’,
‘author-box’,
‘breadcrumbs’,
‘search-form’,
‘post-navigation’,
‘post-comments’,
‘theme-elements’,
// ‘blockquote’,
‘template’,
‘wp-widget-audio’,
‘woocommerce’,
‘social’,
‘library’,
‘reviews’,
‘gallery’,
‘table-of-contents’,
‘html’,
‘star-rating’,
‘sitemap’,
‘theme-site-logo’,
‘theme-site-title’,
‘theme-page-title’,
‘theme-post-title’,
‘theme-post-excerpt’,
‘post-info’,
‘theme-post-featured-image’
];
// CHECK CURRENT USER ROLE AND EXECUTE RESTRICTION IF NEEDED (in thix exemple for editor role)
if( is_admin() && get_current_user_role() == ‘editor’) {
// Remove all the Elementor Free & Pro Widgets listed above
add_action(‘elementor/widgets/widgets_registered’, function($widgets_manager){
global $elementor_widget_blacklist;
foreach($elementor_widget_blacklist as $widget_name){
$widgets_manager->unregister_widget_type($widget_name);
}
}, 15);
// Remove the regular WordPress Widgets from the left-hand Panel in the Live Editor
if ( ! function_exists( ‘ddw_tweak_elementor_remove_wp_widgets’ ) ) :
add_filter( ‘elementor/widgets/black_list’, ‘ddw_tweak_elementor_remove_wp_widgets’ );
function ddw_tweak_elementor_remove_wp_widgets( $black_list ) {
/** Bail early if Elementor not active or tweak not wanted */
if ( ! defined( ‘ELEMENTOR_VERSION’ ) ) {
return $black_list;
}
/**
* Get all registered WordPress widgets, but only the classes
* (= the first-level array keys)
*/
$black_list = array_keys( $GLOBALS[ ‘wp_widget_factory’ ]->widgets );
/** Return black list array for filter */
return (array) $black_list;
}
endif;
}