Archived topic
Is there a way to make the Off Canvas Panel only appear for logged Users?
19 replies · Started by Jonathon on September 9, 2021
I have a use case for the Off Canvas Panel that I think could make the work of my users easier, but the problem is I only want it to show if the user is logged in and part of some user roles. Is there a way to achieve this with a hook or filter?
Hi Jonathon,
Are you trying to hide some of the menu items in off canvas panel or the entire off canvas panel?
If you can link us to your site, that'll be helpful.
Let me know :)
I want to hide the "button" that opens the Off Canvas Panel to only show for logged users of a certain role. I'm using the Nav Menu Roles Plugin to do this in the actual menu, but I was wondering if there was a way to do it with the panel because I have one group of users that might benefit from accessing instructions and key features with this panel, but having the panel there empty isn't useful for all the other users.
Does that make sense?
I don't have the panel enabled on the site at the moment. I'm on a fact finding mission.
Jonathon
I wonder if we can just use CSS to hide the toggle when a user is not logged in.
Can you link me to the page in question?
Sure, but again I don't have the panel enabled at the moment.
We are just trying to hide the toggle right?
If so try this CSS:
body:not(.logged-in) .main-navigation .menu-bar-item.slideout-toggle {
display: none;
}
I tried the provided CSS on another site that is currently live and using the panel. It didn't work. The button for the panel while logged out was still visible. I'll provide the site in the Private information section. If you actually need login details I can generate them for this smaller site.
My steps:
- Copied CSS from this post.
- Pasted it into Customizer additional CSS
- Loaded site in different browser with not logged in user.
- Forced my host to clear the cache, but the button was still there.
Any other ideas?
Ahh you are referring to the desktop view.
Try the edited CSS above:
https://generatepress.com/forums/topic/is-there-a-way-to-make-the-off-canvas-panel-only-appear-for-logged-users/#post-1924777
Okay, I was confused for a second until I realized you had updated the css code snippet. That worked. Thank you.
It's the same forum post but I've edited the code there.
Can I follow up with another level of complexity. Is there any way to set this up to only show by a certain user roles?
Unfortunately not I'm aware of. WordPress doesn't add the user role to the <body> tag as far as I understand.
If you can find a solution to add the user role in then we should be able to adjust the CSS.
Hi,
You could inject the required CSS with a PHP function when the current users role is the one you want. So for this instance you would make the CSS disable it as default and with the function below undo it.
.main-navigation .menu-bar-item.slideout-toggle {
display: none;
}
<?php
$user = wp_get_current_user();
if ( in_array( '<rolename>', (array) $user-roles ) ) {
wp_add_inline_style( '<cssname>', '.main-navigation .menu-bar-item.slideout-toggle{display:block;}' );
}
Thanks for sharing!
No problem. Happy to help the community.