How can one add a toggle button to the post menu and implement an action for that toggle button in a Discourse plugin?

AI-generated summary

In a discussion about adding a toggle button to the post menu in a Discourse plugin, omppatil seeks guidance as a beginner. Lilly directs them to helpful resources, including a post on class connectors for post buttons and beginner guides to plugin development. Arkshine provides code using api.addPostMenuButton to add a button (e.g., ‘coffee’) and api.attachWidgetAction to define its action, noting that Lilly’s resources are also valuable. omppatil asks about file structure and code placement. Lilly reiterates that the linked topics cover these topics and suggests considering a theme component instead of a plugin, sharing additional theme guides and pointing to a GitHub example that demonstrates button addition and action handling.

Hi!,
I’m new to Discourse plugin making and would appreciate some help with adding a toggle button to the post menu and implementing its action in the plugin.

Hi @omppatil perhaps this post might help you get in the right direction:

Also if you are new to Discourse plugin programming:

Hi!

You can use addPostMenuButton

api.addPostMenuButton('coffee', () => {
 return {
   action: 'drinkCoffee',
   icon: 'coffee',
   className: 'hot-coffee',
   title: 'coffee.title',
   position: 'first'  // can be `first`, `last` or `second-last-hidden`
 };
});

Then you can add your action this way:

api.attachWidgetAction("post", "drinkCoffee", function () {
    //
}

EDIT: Got Lillian’d :smile:. It would also be a good idea to follow the guides Lilly posted!

Therefore, what file structure should be implemented, and where should the above code be placed?

Reading the above linked topics should answer your related dev questions. It’s possible you could achieve what you want with a theme component instead of plugin. Thus, I would also add these to your reading list:

also, from the first link I posted: