Skip Navigation

[Resolved] Make a menu visible only to certain logged in users

This support ticket is created 3 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by AlimB3245 3 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1689527

Tell us what you are trying to do? I have a a regular menu and some of the menus and sub-menus are meant for PRIVATE VIEWS/PAGES.. I think I found tutorials on how to make certain pages visible only to certain GROUPS so I think I will manage that.. but I wanted to know if I could have a menu or submenu that would be visible only to these GROUPS.. so I would like to have a LOGIN menu in case you are not logged in and a LOGOUT menu once you are logged in.. other than this I'd like certain SUB MENUS to be visible only to certain users

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site? hidden link

#1690143

Hello, if you're talking about the menus available in wp-admin > Appearance > Menus, then Toolset doesn't have any input on the display of those menu items. If you're talking about a more custom solution where you build out a menu manually with blocks, HTML or using Views, then it might be possible to use conditional blocks to show and hide individual elements in the menu based on login status. We also have the Access Control shortcodes that can be inserted in content editors, which will allow you to restrict content by user role: https://toolset.com/course-lesson/access-control-texts-inside-page-content/

So it depends on which type of menu you are discussing.

#1690849

Hi Christian

Thanks for the update..:-)...

Yes, I was actually referring to the menu created by APPEARANCE ->MENUS.. but as you mentioned that's not possible then I might have to look at the other option..

Basically I need a login and logout link/menu which changes based on the state of the user.

How can I create this using toolset?

Regards,
Alim

#1691787
conditional-2.png
conditional-1.png
blocks-design.png

You could do this with two Toolset Conditional blocks in the Block Editor. The trick here is knowing that a logged-in User will have a User ID of some number greater than zero. A non-logged-in User will have no User ID. So in the first conditional, check to see if the current User ID is greater than zero. If so, you know this is a logged-in User and you will display the Logout link. In the second conditional, check the same thing but choose the "NOT" toggle in the top left corner. You know this is a non-logged in User and you will display the Login link. See the screenshots here for more details about the conditional settings and blocks design.

#1696953

Hi Christian

Thanks for the update..:-)..

Will definitely try this out.. but currently I am stuck with another critical issue which is not allowing me to create any view or update any views.. Shane is trying to help me on that one ..

https://toolset.com/forums/topic/the-wordpress-archive-for-my-custom-post-articles-shows-no-results/#post-1696937

On the issue about the LOGIN button, it sounds like I can create the button/link in a Block Editor.. so I would have to do this on every content template or page.. am I right?.. If I wanted to create just one of this thing (I don't know what to call it.. module,view?) but yes if I were to create one 'login' button which I would want all users to be able to see I would have to put it in the NAV bar or somewhere in the header .. right?

What's the 'best practice' way to do this?

Regards,
Alim

#1704515

Okay I'll stand by for your updates after the other critical issue has been resolved. For the login/logout button, I think you are correct in that adding it to the main menu or site header is the typical solution here. As far as best practice, you're talking about custom theme development, not something Toolset-related, so that's not something I'm trained to support. In general, you would be looking for ways to filter the main menu elements if you want to add the link in the main menu. There is a filter specifically for this called wp_nav_menu_items:
https://developer.wordpress.org/reference/hooks/wp_nav_menu_items/
That developer doc has a User-submitted comment with information about adding different links depending on login status.

Here's another post specifically for login/logout links:
https://premium.wpmudev.org/blog/how-to-add-a-loginlogout-link-to-your-wordpress-menu/

#1704633

Hi Christian

Thanks for the update..:-).. really appreciate the efforts to help guide me in understanding the process..

I think I will go with the WPMU dev link and experiment with it as it's from WPMU where I host my site so it'd be easier to ask for help.

Theoretically it seems they've got what I needed. I would be able to have a LOGIN link displayed normally.. and on the user logging in I would have the LOGOUT link..

I am going to try to make the LOGOUT link into a MAIN MENU with SUB-MENUS in the following structure

MY ACCOUNT will be the MAIN MENU

The Sub menu will be as below

  • My Profile
  • My Zoo
  • My Bakery
  • Logout

I would appreciate if you could advise me or guide me to some links which would help me put the IF condition to My Zoo and My Bakery sub menus.

If the logged in user had the custom-role of ZOOKEEPER then the My Zoo sub-menu would be visible and if the user had the custom-role of BAKER then the My Bakery sub-menu would be visible..

These roles and sub-menus are fictitious references just in case you start to wonder what kind of a website would have a zoo keeper and a baker registered on the same site!!!..;-).. but you never know!..;-)..

As you have mentioned earlier that this could be beyond your scope of 'Toolset Support', I would totally understand if you ask me to look for custom development.. but if possible, please do guide me to some links or some code that could help me achieve this as I plan to try and do this myself.

Have a nice day..:-)..

Regards,
Alim

#1705283

I would appreciate if you could advise me or guide me to some links which would help me put the IF condition to My Zoo and My Bakery sub menus.
You can use the function wp_get_current_user() to get information about the current User including his/her roles.
https://developer.wordpress.org/reference/functions/wp_get_current_user/

This post shows how to test if the User has one specific role:
https://wordpress.stackexchange.com/questions/5047/how-to-check-if-a-user-is-in-a-specific-role

#1708715

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.