How to hide W3TC menu items from non-Administrators

If you need to hide the W3 Total Cache sidebar and admin bar menu item links from non-Administrators you can use the following in your functions.php file:

/**
 * Hide W3 Total Cache sidebar menu from non-Administrators
 */
function hide_w3tc_menu_sidebar() {
    if ( !current_user_can( 'manage_options' ) ) {
        remove_menu_page( 'w3tc_dashboard' );
    }
}
add_action( 'admin_menu', 'hide_w3tc_menu_sidebar', 11 );
 
/**
 * Hide W3 Total Cache admin bar menu from non-Administrators
 */
function hide_w3tc_menu_admin_bar() {
    global $wp_admin_bar;
 
    if ( !current_user_can( 'manage_options' ) ) {
        $wp_admin_bar->remove_menu('w3tc');
    }
}
add_action( 'wp_before_admin_bar_render', 'hide_w3tc_menu_admin_bar' );
 
/**
 * Hide W3 Total Cache "Purge from cache" from Page and Posts lists
 */
function remove_purge_from_page_cache_link( $actions, $post ) {
  if ( !current_user_can( 'manage_options' ) ) {
    unset($actions['pgcache_purge']);
 
    return $actions;
  }
}
add_filter('post_row_actions', 'remove_purge_from_page_cache_link',1000,2);
add_filter('page_row_actions', 'remove_purge_from_page_cache_link',1000,2);

This will effectively display the W3 Total Cache menu items to users only with Administrator role capabilities.

Post written by Ed Reckers

Founder and lead web development consultant at Red Bridge Internet : San Francisco WordPress Developers and Consultants.

Leave a Reply