admin管理员组文章数量:1122832
const { currentUser } = useSelect(
( select ) => ( {
currentUser: select("core").getCurrentUser()
} ),
[]
);
This gives me a user object, with the user id. But no role. How can I get the role?
Thank you
const { currentUser } = useSelect(
( select ) => ( {
currentUser: select("core").getCurrentUser()
} ),
[]
);
This gives me a user object, with the user id. But no role. How can I get the role?
Thank you
Share Improve this question asked Sep 17, 2024 at 19:11 Justin WylllieJustin Wylllie 374 bronze badges 01 Answer
Reset to default 0You can achieve this with the help of useSelect
hook and select('core').getCurrentUser()
here is the sample component for you.
import { useSelect } from '@wordpress/data';
const CurrentUserComponent = () => {
const { currentUser } = useSelect((select) => ({
currentUser: select('core').getCurrentUser(),
}), []);
// Here we are checking if currentUser is available or not.
if (!currentUser) {
return <p>Loading...</p>;
}
// Here we are accessing the roles.
const roles = currentUser.roles || [];
return (
<div>
<h2>Current User Roles:</h2>
<ul>
{roles.map(role => (
<li key={role}>{role}</li>
))}
</ul>
</div>
);
};
export default CurrentUserComponent;
本文标签: wp adminCan I get the role of the currentUser in modern WordPress React
版权声明:本文标题:wp admin - Can I get the role of the currentUser in modern WordPress React? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290054a1928433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论