admin管理员组文章数量:1122832
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!
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!
Share Improve this question edited Mar 27, 2018 at 19:18 Dave Romsey 17.9k11 gold badges55 silver badges70 bronze badges asked Mar 27, 2018 at 17:56 Mark NielsenMark Nielsen 113 bronze badges 2 |1 Answer
Reset to default 0You can use the current_user_can()
function to test the current user's role. I would also recommend making the custom field protected by default and allowing access only to the proper users.
In theory, something like this should work:
function my_exclude_custom_fields( $protected, $meta_key) {
if ( in_array( $meta_key, array( 'email', 'text' ) ) ) {
if ( current_user_can( 'udlejer' ) ) {
// meta key is not protected for user role 'udlejer'
return false;
} else {
// make the meta keys protected by default
return true;
}
}
return $protected;
}
add_filter( 'is_protected_meta', 'my_exclude_custom_fields', 10, 2 );
本文标签: Hide custom fields by user39s role
版权声明:本文标题:Hide custom fields by user's role 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281235a1926252.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$currentUser
coming from? Your codes formatting is broken, can you fix it? Code is easier to read/fix with good formatting :) – Tom J Nowell ♦ Commented Mar 27, 2018 at 18:12