Disclaimer: This article is provided for instructional purposes only. Zendesk does not support or guarantee the code. Please post any issues you have in the comments section or try searching for a solution online.
Question
How can I hide or show HTML based on user's role or group?
Answer
See the two sections below:
Hide or show HTML based on user's role
If the information you're hiding is sensitive, this may not be a workable solution for you since the full text can be shown by Viewing Source in the browser.
However, find below a sample code to implement in your Help Center and a screencast showing where to put it.
<div class="anonymous">
<center><h1>Welcome Anonymous User!</h1></center>
</div>
<div class="end_user">
<center><h1>Welcome End-User!</h1></center>
</div>
<div class="agent">
<center><h1>Welcome Agent!</h1></center>
</div>
<div class="manager">
<center><h1>Welcome Manager!</h1></center>
</div>
Insert the following code under the CSS template
/*hide role-specific div html*/
div.anonymous, div.end_user, div.agent, div.manager {
display: none;
}
Insert the following code under the JS template
// Show div html based on role
if (HelpCenter.user.role=="anonymous"){
$("div.anonymous").show();
}
if (HelpCenter.user.role=="end_user"){
$("div.end_user").show();
}
if (HelpCenter.user.role=="agent"){
$("div.agent").show();
}
if (HelpCenter.user.role=="manager"){
$("div.manager").show();
}
Hide or show HTML based on user's group
If you want to show/hide HTML based on groups, the process is the same as above. However, in the JS template, use the code below.
var groups = HelpCenter.user.groups;
for (var i = 0; i < groups.length; i++) {
if (groups[i].name == 'L2 Support' && groups[i].isActive == true) {
$("div.l2").show();
}
}
For more information about editing the template of your Help Center, see Customizing your Help Center theme (Guide Professional and Enterprise).
37 Comments
Aaron - one last bit, that we did when we went through a user tag phase for our beta rollout of the community aspect in Zendesk was the bulk update with CSV for users, that way we didn't have to manually go in and update one at a time.
Thanks for sharing this solution Jeremy!
Hello ! This oslution seems to not work anymore with the new template version !
https://support.zendesk.com/hc/en-us/articles/360043187273#topic_llb_t5g_skb__section_rxc_dbd_tkb
Does anyone know how to show/hide based on a users role by using Curlybars if statements? Is this possible?
Simon Agliati the only way to do that is documented above here.
Hello!
Does anyone have any experience or knowhow how to show the Community for a specific User Segment only? I have a User Segment ''Staff'' which are the employees or our company. Since we first want to roll-out the Community internal I am looking for a way to have it shown only to that specific User Segment. I do think the User Segment has an ID, maybe I can target that using JavaScript?
Any help is much appreciated. Thanks and have a great New Year!
Ron
Hi Ron,
All you need to do is set the visibility of all of the topics in your community to that internal user segment.
You may need to hide the link to the community from en-users on your Help Center with a little custom code, otherwise they'll click it and show up to an empty community. Then just make sure you give your employees a direct link to the community as they won't be able to navigate to it.
Please sign in to leave a comment.