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.

3 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.

1 Like

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.