• Resolved kaprikorn

    (@kaprikorn)


    Basically what I’m trying to do is to remove an element using CSS for a peculiar user role. I tried the following snippet, but no luck.

    $user = wp_get_current_user();
    if ( in_array( 'customer', (array) $user->roles ) ) {
     add_action( 'wp_head', function () { ?>
    <style>
    .CSSselector: {display: none;}
    </style>
    <?php } );}

    Please help. Thanks!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    You need to include the user check within the acton itself – when snippets first run, it’s too early to be checking user data.

    add_action( 'wp_head', function () {
    	$user = wp_get_current_user();
    
    	if ( ! in_array( 'customer', (array) $user->roles ) ) {
    		return;
    	}
    	?>
    	<style>
    		.CSSselector: {display: none;}
    	</style>
    	<?php
    } );
    Thread Starter kaprikorn

    (@kaprikorn)

    Hi Shea. Thank you for your swift reply. It is very much appreciated. Unfortunately the snippet does not work.

    However this part of the code as per the CSS snippet example in your plugin does work. So integrating the user role condition in it is the issue.

    add_action( 'wp_head', function () {
    	?>
    	<style>
    .CSSselector {display: none;}
    	</style>
    	<?php
    } );
    Plugin Author Shea Bunge

    (@bungeshea)

    Are you certain that ‘customer’ is the correct name of the role?

    If you can target users by capabilities instead, I’d recommend using the current_user_can() function.

    Thread Starter kaprikorn

    (@kaprikorn)

    Actually ‘vendor’ is the correct name of the role. I am using Dokan and trying to remove the inventory element on the product page in the vendor dashboard. The CSS class is inventory.dokan-edit-row

    Plugin Author Shea Bunge

    (@bungeshea)

    Is the vendor dashboard on the site’s front-end, or in the administration area? If it’s the latter, you need to use the admin_head hook:

    add_action( 'admin_head', function () {
    	$user = wp_get_current_user();
    	if ( ! in_array( 'vendor', (array) $user->roles ) ) return;
    	?>
    	<style>
    
    	.dokan-edit-row { display: none; }
    
    	</style>
    	<?php
    } );
    Thread Starter kaprikorn

    (@kaprikorn)

    1) I have checked all the vendor’s user role capabilities and the current_user_can() function is not suitable for the case.

    2) The vendor dashboard is on the site’s front end.

    Plugin Author Shea Bunge

    (@bungeshea)

    It looks like Dokan includes a dokan_is_user_seller function for doing this. Should be more reliable than manually checking the role.

    add_action( 'wp_head', function () {
    	$user = wp_get_current_user();
    	if ( ! dokan_is_user_seller( $user->ID ) ) return;
    	?>
    	<style>
    
    	.dokan-product-inventory { display: none; }
    
    	</style>
    	<?php
    } );
    Thread Starter kaprikorn

    (@kaprikorn)

    This snippet still does not work. It does hide the element though, no matter the user role. This was not the case with your previous snippet.

    Plugin Author Shea Bunge

    (@bungeshea)

    It looks like the name of the role is actually seller. Try this:

    add_action( 'wp_head', function () {
    	$user = wp_get_current_user();
    	if ( ! in_array( 'seller', (array) $user->roles ) ) return;
    	?>
    	<style>
    
    	.dokan-product-inventory { display: none; }
    
    	</style>
    	<?php
    } );
    Thread Starter kaprikorn

    (@kaprikorn)

    It’s finally working! Thank you so much Shea for your persistence and help in getting to this solution. You made my day and deserve 5 stars! Take care.

    Plugin Author Shea Bunge

    (@bungeshea)

    Glad to hear that it’s all working!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Trying to create a conditional CSS snippet based on user role’ is closed to new replies.