Force users to pick category

I want users to manually pick a category when creating a new post.
I already checked the topics:

https://meta.discourse.org/t/invite-people-to-choose-category-before-create-a-new-topic/214077
https://meta.discourse.org/t/require-category-selection-before-writing-topic/66236/15

but none of them seem to address this.
I have Allow uncategorized topics unchecked.

Is the issue that a category comes pre-filled? If so, you may be looking for the “Default composer category” site setting.

4 Likes

Yes, that was the issue.

I just Reset that setting and it’s working as expected. Thanks! :raising_hands:

I noticed that when that setting is set to no category, it doesn’t allow the user to click and type and it shows a message to pick a category, but sometimes people don’t immediately read the text. Do you by any chance know if it’s possible to show some “pop up” when the user clicks the message area, before picking a category?


EDIT: After trying many options that Claude and ChatGPT offered for this, I ended up just removing the “Disabled” property. So users can still click and write their message, but when no category is selected and they try to submit, they see the red/orange error asking to pick a category. This is actually a better behavior, in my opinion.

For others trying to do the same, here’s the Javascript I added to the <head> tab in a new component I created called Unlock Composer:

<script type="text/discourse-plugin" version="0.8">
  const observer = new MutationObserver(() => {
    const textarea = document.querySelector(".d-editor-input[disabled]");
    if (textarea) {
      textarea.removeAttribute("disabled");
      textarea.setAttribute("placeholder", "Select a category before submitting");
      textarea.style.cursor = "text";
    }
  });

  observer.observe(document.body, { childList: true, subtree: true });
</script>

I’m not a developer. This was just what ChatGPT suggested and it works. If anyone sees how this can be improved, please share.

Would it be possible to share a screenshot of this in action please.

When I click New Topic, it shows this:

You can see there’s no default category. Now, I can type my message (unlike the default behavior, which locks the message area), even without a category being selected:

If I try to submit, I see this:

Hope this helps.

1 Like

@alltiagocom excellent, thanks!

1 Like

I think this is normally the default behaviour. :thinking: Do you have a topic template set up by any chance?

You’re welcome!

I just asked ChatGPT to see if there’s a way to simplify the script a little bit more and it removed the first line, related to the API. I updated my post with the final script, in case you want to use it.

1 Like

No, no template.

But I believe this is not the default behavior, because even the original placeholder says something like “Select a category before typing here”, and the cursor changes to an arrow with a :prohibited: symbol. It doesn’t allow me to click and type, unless I pick the category.

That’s what I would expect if you have a topic template set up on one of your categories. Without any set it should allow you to enter the composer text area before setting a category. It blocks it when using topic templates so people don’t accidentally bypass that template by typing in the composer first and then selecting a category.

I’ve just had a quick test run on my site, and with no default category or topic templates I can enter the composer area without being blocked until I select a category.

2 Likes

Weird… I just disabled the component I created, tried it again, and now it lets me type. It’s not locked :confused:

Would it be some cache issue after I set it to (no category) or something? Cache on the server itself, because on both Chrome and Firefox, with different user accounts, I got the same behavior. I’m using Cloudflare and I noticed that sometimes there’s issues with my forum that seem to be related with Cloudflare’s cache. Again, I’m no expert, so I’m just guessing…

Ok, I think I spoke too soon…
A few minutes later, it’s back to the other behavior. It’s locked. And when I Inspect the composer, I see this:

I guess I will just stick to the Component, then.

2 Likes

If it gets it working as you want it to then :chefs_kiss:

Out of curiosity, if you have the data explorer plugin installed, could you run this just to see what it says:

SELECT id as category_id, topic_template
from categories

I’ve just been testing this out on another site (which happens to be German as the default locale) and we spotted that there was a cheeky template added to the Uncategorised pseudo category which seems to have caused the same behaviour on that site.

I just noticed that on the Obsidian forum, the behavior is the same. It’s locked until I pick a category, so it’s probably the new behavior?

I do not have the data explorer plugin, sorry.

I don’t think so. :thinking: On both recently updated sites I’ve tested this on the behaviour is the same as I’d expect (no default category or topic templates = free to type without choosing a category first).

What we couldn’t work out was where the Uncategorized topic template came from on the German locale site. A sneaky auto-added one in there for whatever reason would break the intended functionality without anyone realising.

Do you have one in yours? (And if you could share your default locale that would be useful too)

/c/uncategorized/edit/topic-template

ETA: As an alternative to the date explorer, you can also filter your staff logs to 'topic template’ and see if there’s any extra info in there.

(I’ll also add the filtered link here that Moin shared below)

/admin/logs/staff_action_logs?filters=%7B%22subject%22%3A%22topic_template%22%7D

It’s a great plugin, I highly recommend it. :slight_smile:

3 Likes

I think it’s a little difficult to filter for that in the interface because you can only filter for actions and not for subjects. There can be a lot of logs for the “change category settings” action. But you can add a subject filter to the URL: /admin/logs/staff_action_logs?filters=%7B%22subject%22%3A%22topic_template%22%7D

2 Likes

i would do yourself a big favor and install it because it is super useful. :slight_smile:

3 Likes

Weird that Obsidian’s forum behaves the same way as mine. Go figure…

Also empty. I don’t think I have any templates, actually, anywhere.

1 Like

Thank you Lilly (and thank you @JammyDodger) for sharing this plugin.
Will definitely take a look at it.

1 Like

I think you should be able to see the default behaviour on try.discourse.org, if you’re still curious. No, I was mistaken. Try has a default category set.

It’s the forgotten ones I was hoping to unearth. :slight_smile: But if there’s not an auto-added one to Uncategorised then I think this rules out a potential bug, so that’s good. :+1:

There’s also the staff logs too (using the link Moin shared), but you seem happy with your workaround so I’ll stop pumping you for more info. :slight_smile: