Search by

helsingborg-stad / wpmu-disable-media-delete

sebastianthulin

Disable media delete by config. Typically useful in staging environments using same media files as production.

Package info

github.com/helsingborg-stad/wpmu-disable-media-delete

Type:wordpress-muplugin

pkg:composer/helsingborg-stad/wpmu-disable-media-delete

Statistics

Installs: 65

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.2.4 2026-12-01 12:13 UTC

This package is auto-updated.

Last update: 2026-12-01 12:15:40 UTC


README

Prevents media (attachments) from being deleted when a constant is set in wp-config.php.

Why

Staging and test environments often share uploaded files with production, for example through the same S3 bucket or network mount, while having their own database copy. Deleting an image on staging would then remove the file that production still uses.

With this plugin, you can turn off media deletion in those environments. When the constant is not defined, the plugin does nothing.

Installation

Install it with Composer, as a must-use plugin (recommended) or as a regular plugin:

composer require helsingborg-stad/wpmu-disable-media-delete

If you install from source, run composer install --no-dev in the plugin directory.

Usage

Add this to wp-config.php (or a config file it includes) in the environment where deletion should be blocked:

define('WPMU_DISABLE_MEDIA_DELETE', true);
  • Only a truthy value enables blocking. If the constant is missing, false, 0 or '', WordPress works as normal and the plugin registers no hooks.
  • In a multisite network, the constant applies to every site in the network.

What happens when deletion is blocked

The plugin returns false from the pre_delete_attachment filter. wp_delete_attachment() then aborts, and both the attachment post and its files are kept.

Where What the user sees
Media library A warning notice saying that media deletion is disabled.
Media library, list view A "Deletion not allowed" page with a link back.
Media library, grid view and media modal An alert with the message. In the library the page reloads so the item shows up again.
REST API Core's error response (the attachment cannot be deleted).
AJAX A JSON error response with the message and HTTP status 403.
Code calling wp_delete_attachment() The call returns false.
WP-CLI, cron and front end No message. wp_delete_attachment() returns false and the script or request continues.

The messages are translatable (text domain wpmu-disable-media-delete). Swedish is included.

Limitations

  • The filter runs last (priority PHP_INT_MAX). If another filter has already stopped the deletion, for example a theme that moves media to the trash instead, its result is kept and no message is shown.
  • Only attachment deletion is blocked. Moving media to the trash (when MEDIA_TRASH is enabled) is still possible, because it does not remove any files.
  • Files removed in other ways are not affected, for example by image editing, regenerating thumbnails, other plugins calling wp_delete_file() directly, or direct filesystem or bucket access.

Development

composer install
composer test        # PHPUnit
composer lint        # Mago linter
composer format      # Mago formatter

Tests live next to the class they cover (Foo.php, FooTest.php) in source/php/.

Translations

composer make-pot    # Extract strings from source/php to languages/*.pot
composer update-po   # Merge the .pot into the .po files
composer make-mo     # Compile the Swedish .mo file

Continuous integration and releases

  • Pull requests to main run lint and tests on PHP 8.3 and 8.4 (.github/workflows/test.yml).

  • Pushes and merges to main create a new release (.github/workflows/release.yml). The version is calculated by GitVersion from Conventional Commits:

    • feat: bumps the minor version.
    • fix:, docs:, chore:, refactor:, test:, build:, ci:, perf:, style: and revert: bump the patch version.
    • ! after the type (e.g. feat!:) or a BREAKING CHANGE: footer bumps the major version.

    The workflow writes the version to the plugin header and commits it to main, then creates a tagged GitHub release with generated release notes.

License

MIT