Is there a way to show/hide/change menu entries (at the top of the web site) based on the user’s role? That is, having one menu for “generic users” (who have not logged in) and “members” (who have created an account and logged in). Thanks!
Hi,
Yes there is.
There are three roles, Administrator, Member (signed in user) and a visitor.
Check out this reference to determent witch user is in your site.
And this to show and hide element
Good luck!
Roi
Thanks! I do use this on many elements and in various use cases. But can you do it for the site menus at the top? Thanks!
You can create a couple of menus according to your needs.
Roi
Yes, I understand. What I am looking for is a pointer to the APIs so I can “turn on” and “turn off” menu items based on the users role. Thanks!
HI dmz,
Site menu itself does not have a Wix Code API.
You can, of course, create you own menu using buttons.
Liran.
@Lian - Thanks - that is what I thought. Any plans to introduce this feature? Seems like a pretty useful thing?
Or is there example code on how to “roll your own”?
Thanks!
No ETA for this, you can submit a feature request here .
Regarding an example… it should be pretty simple… here’s a skeleton:
import wixUsers from 'wix-users';
$w.onReady(fucntion(){
const currentUser = wixUsers.currentUser;
if (currentUser.loggedIn) {
if (currentUser.role === 'Admin') {
//show or hide elements here:
$w('#someButton').show();
}
if (currentUser.role === 'Member') {
$w('#someButton').hide();
}
}
});
Liran.
Okay
Is there a way to make more roles? Because I am trying to make a ‘Staff Portal’ and I want to hide the button to access the staff portal to every member but staff members.
You would be much better simply posting this as a new forum post instead of adding it to an old one.
As for member roles, you can easily add whatever roles you want for your site.
https://support.wix.com/en/article/creating-member-roles-6943237
https://support.wix.com/en/article/managing-your-member-roles
As your question does not involve the use of code, for which this Corvid Forum is for, you would have been better suited directing your question to Wix Support themselves or reading through the Wix Support pages for member roles.
@givemeawhisky Is there any way to hide the button?
If the user is a site member then you can just check the current users role through Wix Users API.
https://www.wix.com/corvid/reference/wix-users.html
https://www.wix.com/corvid/reference/wix-users.html#currentUser
Something like this which will show it to all Staff role members, which is the opposite of what you want but you can get the idea!
import wixUsers from 'wix-users';
$w.onReady(function () {
var roleName;
let currentUser = wixUsers.currentUser;
currentUser.getRoles()
.then( (roles) => {
var firstRole = roles[0];
roleName = firstRole.name;
if(roleName === "Staff"){
$w('#yourButton').show();
}
})
})
Or you simply go back up this post and use the code example already provided to you from Liran.
import wixUsers from 'wix-users';
$w.onReady(function(){ // note function was spelt wrong on Liran's //
const currentUser = wixUsers.currentUser;
if (currentUser.loggedIn) {
if (currentUser.role === 'Admin') {
//show or hide elements here:
$w('#someButton').show();
}
if (currentUser.role === 'Member') {
$w('#someButton').hide();
}
}
});
@givemeawhisky Hi, thank you for your help with this! This idea was exactly what I was looking for. However, I was not able to implement this method as it returns a “parsing error : unextected token )” in line 13. any idea how to fix it? this was my implementation based on your sceleton:
1 import wixUsers from 'wix-users';
2
3 $w.onReady(function () {
4 var roleName;
5 let currentUser = wixUsers.currentUser;
6
7 currentUser.getRoles()
8 .then( (roles) => {
9 var firstRole = roles[0];
10 roleName = firstRole.name;
11 if(roleName === 'WeddingGuests'){
12 $w('#RSVP').show();
13 });
14 })
If you need anymore help please add a new forum post instead of bumping up this old thread, thanks.
import wixUsers from 'wix-users';
$w.onReady(function () {
var roleName;
let currentUser = wixUsers.currentUser;
currentUser.getRoles()
.then((roles) => {
var firstRole = roles[0];
roleName = firstRole.name;
if (roleName === 'WeddingGuests') {
$w('#RSVP').show();
}
})
})