admin管理员组文章数量:1391964
i am trying to make a simple custom field (made without a plugin) readonly or disabled for a specific user role in functions.php, is this possible ?
thanks
i am trying to make a simple custom field (made without a plugin) readonly or disabled for a specific user role in functions.php, is this possible ?
thanks
Share Improve this question asked Mar 1, 2020 at 16:07 sofdesignesofdesigne 12 bronze badges 5 |1 Answer
Reset to default 1First, you can get the current user object like so: $user = wp_get_current_user();
.
The $user
object would have a roles
property containing the user's roles, so you can do the following to check if the current user has a specific role: in_array( 'role_slug', $user->roles )
.
And then in your field HTML, you can use the disabled()
and readonly()
functions to easily add the disabled
and readonly
attributes:
<?php
// Get the current user object.
$user = wp_get_current_user();
// Check if the associated user has a specific role.
$has_role = in_array( 'administrator', $user->roles );
?>
<input name="some_name" <?php disabled( true, ! $has_role ); ?> />
<input name="some_name2" <?php readonly( true, ! $has_role ); ?> />
And the above would make the field be disabled/readonly if the current user doesn't have the role administrator
(note the ! $has_role
).
本文标签: customizationhow to make a custom field readonly or disabled by user role
版权声明:本文标题:customization - how to make a custom field readonly or disabled by user role? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744702851a2620661.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<input>
field? – Sally CJ Commented Mar 1, 2020 at 16:53