Search code examples
phphtmlwordpresshide

Hiding DIV from menu based on user role / capabilities Wordpress


I have a wordpress site with 2 user roles, customer and vendor. I want to hide the Vendor Dashboard from the menu bar from Customers. The code for this theme is in the header-aside.php file so a plugin won't work to hide this element. I have tried the following code which hides this from everyone, not just customers, so I'm not sure what I have wrong here.

     <?php if (current_user_can(‘read’)) { ?>
        <div class="dashboard-icon">
            <a id="header-button" href="/creator-dashboard/" class="header-button boss-tooltip" data-tooltip="<?php _e( 'Creator Dashboard', 'onesocial' ); ?>"><i class="fas fa-tachometer-alt"></i></a>
        </div>
    <?php } ?>

Note I have the capability 'read' in there as a test since I am unable to get it to show. But the actual capability a vendor has is 'edit_products'


Solution

  • You can check the user roles inside the WP_User object, which is returned by the function wp_get_current_user (). So you may show Dashboard only for Vendor by that code:

    <?php 
    $user = wp_get_current_user();
    if ( in_array( 'vendor', $user->roles ) ) {
    ?>
        <!-- Any HTML what you need to hide from "Customers" and show for "Vendor" -->
        <div>Vendor Dashboard</div>
    <?php 
    }
    ?>