Admin only button in header?

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?

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.

6 Likes

That was exactly what I needed! Thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.