Admin only button in header?

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