skip to Main Content

I’m developing a website in wordpress using Woocommerce and WC Vendor Marketplace plugin. I want to show content on certain page user role wise in wordpress website.

For example.
If i’m login as a vendor then it shows content “A” and if i’m login as a customer then it hides content “A”.

Here i’m customize my own code for this but not work.

    <?php 
    $userss =get_user_meta( $vendor_id, 'wp_capabilities', true );
    if ($userss='vendor') {
        echo'<li id="menuuu"><a href="https://stackoverflow.com" class="button7" style="background- color:#CCCCCC; color:#000000">Back To Dashboard</a>';
        echo'</li>';
    }                 
    ?>               
<?php endif; ?>

In above code ‘wp_capabilities’ is for user role in wordpress. In my code, user roles are ‘vendor’ and ‘customer’. I’m really weak in php. So please help me.

2

Answers


  1. In wp_capabilities data store as serilized array of user role instead of this you can use current_user_can for more information you can refer to this.
    You can change your code as below:

    if (current_user_can('vendor')) {
        echo'<li id="menuuu"><a href="https://stackoverflow.com" class="button7" style="background- color:#CCCCCC; color:#000000">Back To Dashboard</a>';
        echo'</li>';
    }
    ?>               
    <?php endif; ?>
    
    Login or Signup to reply.
  2. I know this has been marked answered, however using current_user_can() with roles is unreliable and even says as much in the codex.

    While checking against particular roles in place of a capability is supported in part, this practice is discouraged as it may produce unreliable results.

    It’s better to use our built in methods to determine if the current user is a vendor.

    <?php if ( WCV_Vendors::is_vendor( $vendor_id ) ) : ?>
    <li id="menuuu"><a href="https://stackoverflow.com" class="button7" style="background- color:#CCCCCC; color:#000000">Back To Dashboard</a></li>
    <?php endif; ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search