Hide custom fields by user's role
I've tried reading all the questions about this topic, and configured the code to fit my needs, but can't seem to get it to work. This is the code I have now:
function my_exclude_custom_fields( $protected, $meta_key, $user ) {
$user = wp_get_current_user();
if ( in_array( 'udlejer', (array) $user-roles ) ) {
if ( in_array( $meta_key, array( 'email', 'text' ) ) ) {
return true;
}
}
return $protected;
}
add_filter( 'is_protected_meta', 'my_exclude_custom_fields', 10, 2 );
It doesn't make any change. Even though I'm logged in with another role than udlejer
, I can still see these custom fields in front-end.
The custom fields are on a product page created by WooCommerce. Do I then need to include more to the code then? Hope you guys can help!
Topic user-roles custom-field Wordpress
Category Web