What's the best way to store data with a plugin?

If you only need to store a single Topic ID (like a configurable value), the simplest Discourse-native way is using a SiteSetting.
You also get a built-in admin UI automatically.

config/settings.yml:

plugins:
  my_plugin_enabled:
    default: true
    client: false

  my_plugin_topic_id:
    default: 0
    client: false
    type: topic      # gives you a topic selector in admin UI

In your Ruby plugin code:

topic_id = SiteSetting.my_plugin_topic_id
topic = Topic.find_by(id: topic_id)

If you prefer to store it programmatically (not exposed as a setting),
PluginStore is also fine for a single key-value:

store = PluginStore.new("my_plugin")
store.set("topic_id", some_topic_id)

topic_id = store.get("topic_id")
3 Likes