I want to add a button to the header of my site that is only visible to admins. Is this possible? Does discourse have conditional code snippets?
AI-generated summary
The discussion revolves around adding an admin-only button to the site header. shaundefense inquired about the possibility of using conditional code snippets to achieve this. simon suggested two approaches: using the staff class added by Discourse to the body tag, which would make the button visible to both admins and moderators, or adding a custom admin class using JavaScript to make the button visible only to admins. shaundefense confirmed that simon’s solution met their requirements.
Discourse adds a staff class to the body tag when a staff user is viewing the site. If you are ok with the button being visible to admins and moderators, you could hide the button with CSS and then make it visible when the staff class is present.
If only having the button visible to admins is a requirement, you will need to use some javascript to determine that the user is an admin. The easiest approach to that would be to add an admin class to the body and then make the button visible when the admin class is present:
<script type="text/discourse-plugin" version="0.8.23">
let currentUser = api.getCurrentUser();
if (currentUser && currentUser.admin) {
$('body').addClass('admin');
}
</script>
If hiding the button with CSS isn’t secure enough for your use case, you will need to do something a little more involved to insert the button with javascript.
That was exactly what I needed! Thank you!