The discussion revolves around showing custom HTML in the header to members of a specific group in Discourse. jord8on initiates the conversation, seeking a solution to display certain buttons only to members of specific groups. jord8on finds a similar article for the Zendesk platform, which seems relevant.
smtreey provides a JavaScript code snippet that uses the api.getCurrentUser() function to retrieve the user’s groups and conditionally add classes to the #after-header element. This allows for CSS-only changes to be made based on the user’s group membership.
Falco suggests installing a component that adds the current user’s groups as classes to the page body, making it easier to make CSS-only changes.
Paracelsus shares a code snippet from ChatGPT that demonstrates how to exclude specific groups from script execution. The code uses the api.getCurrentUser() function and checks if the user belongs to any excluded groups before running the custom script logic.
It also seems to work if you replace api.onPageChange(() => {
with $( document ).ready(function() {
and then the script won’t load on every page, but I never know for sure where I need api.onPageChange(() => { .
I’ve been looking for something similar, but excluding some groups, so I gave it a try with ChatGPT, and the answer was:
To create an HTML theme in Discourse that triggers a script excluding specific groups, you’ll need to use Discourse’s theme customization options along with JavaScript to handle group exclusions. Discourse provides api.getCurrentUser() to retrieve user details, including their groups, which can be useful to conditionally trigger your script.
Here’s an example of how you might implement this in the </head> section of your theme or component:
<script type="text/discourse-plugin" version="0.8">
api.onPageChange(() => {
// Retrieve current user details
const currentUser = api.getCurrentUser();
if (currentUser) {
// List of group names you want to exclude from script execution
const excludedGroups = ['group1', 'group2'];
// Get the user's groups
const userGroups = currentUser.groups.map(group => group.name);
// Check if the user belongs to any excluded group
const isExcluded = excludedGroups.some(group => userGroups.includes(group));
if (!isExcluded) {
// Your custom script logic here
console.log('Script is executed for this user.');
// Example: trigger any custom logic for non-excluded users
// customScriptFunction();
} else {
console.log('User is in an excluded group, script not executed.');
}
}
});
</script>
Explanation:
api.onPageChange(): Runs the script whenever the page changes.
api.getCurrentUser(): Fetches the currently logged-in user’s details.
excludedGroups: List the groups you want to exclude.
userGroups.includes(group): Checks whether the user belongs to any excluded group.
Conditional script execution: The script will only run if the user is not part of any excluded group.
You can replace the customScriptFunction(); with whatever script you need to run. Let me know if you need any further adjustments!
I mean, my mind is completely blown away by this . I haven’t tried it yet, but is the ChatGPT code correct?