### A plugin to prevent unpublishing of content in DatoCMS

(Image content)

[

**This is a Community Plugin!** Learn how create your own plugin, or copy and remix existing ones in our documentation

](https://www.datocms.com/docs/plugin-sdk/introduction.md)

## Prevent Unpublish DatoCMS Plugin

This plugin prevents unpublishing of singleton models in DatoCMS unless the user's role is explicitly allowed. It uses the `onBeforeItemsUnpublish` event hook to intercept unpublish actions and block them for unauthorized roles.

## Features

-   Blocks unpublishing of models for non-allowed roles
-   Allowlist of roles is configurable via plugin settings (comma-separated role IDs)
-   Shows an alert to the user if the action is blocked

## Setup

1.  **Install dependencies**
    
    Go to the plugin directory:
    
    ```sh
    cd prevent-unpublishpnpm install
    ```
    
2.  **Start the development server**
    
    ```sh
    pnpm dev
    ```
    
3.  **Register the plugin in DatoCMS**
    
    -   Go to your DatoCMS project \> Settings \> Plugins \> Add new plugin
    -   Use the local development URL provided by `pnpm dev` (usually `http://localhost:5173`)
4.  **Configure allowed roles**
    
    -   In the plugin settings, enter a comma-separated list of role IDs in the `roleIds` field (e.g. `123,456,789`).
    -   Only users with these role IDs will be able to unpublish singleton models.

## References

-   [Prevent unpublishing of singleton models (DatoCMS Community)](https://community.datocms.com/t/prevent-unpublishing-of-singleton-models/665/6)
-   [DatoCMS Plugin SDK: Event Hooks](https://www.datocms.com/docs/plugin-sdk/event-hooks.md#onBeforeItemsUnpublish)
-   [DatoCMS Content Management API: Item Type](https://www.datocms.com/docs/content-management-api/resources/item-type.md)

## Development

-   The main logic is in `src/main.tsx`.
-   The plugin uses the DatoCMS Plugin SDK and React UI components.

---

For more details, see the code and comments in `src/main.tsx`.