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
} );
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
} );
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.
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
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
} );
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.
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
} );
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.
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
} );
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.
Glad to hear that it’s all working!