Skip to content

Add wp media replace subcommand#240

Merged
swissspidy merged 12 commits intomainfrom
copilot/add-media-replace-subcommand
Mar 22, 2026
Merged

Add wp media replace subcommand#240
swissspidy merged 12 commits intomainfrom
copilot/add-media-replace-subcommand

Conversation

Copy link
Contributor

Copilot AI commented Mar 19, 2026

Adds a wp media replace <attachment-id> <file> subcommand to swap the underlying file of an existing attachment while preserving its identity (ID, title, alt text, caption, etc.).

Changes

  • src/Media_Command.php — New replace() method:

    • Accepts a local path or URL as the replacement file (same handling as import)
    • Sideloads the new file into the uploads directory via wp_handle_sideload()
    • Cleans up temp file on sideload failure to prevent leaks in the system temp dir
    • Deletes old thumbnails, the previous full-size file, and (on WP 5.3+) the original pre-scaled image to avoid orphaned files in uploads
    • Updates MIME type via wp_update_post() with $wp_error=true for proper error detection
    • Validates wp_generate_attachment_metadata() return value before updating; preserves existing metadata on failure
    • Refreshes _wp_attached_file meta and regenerates attachment metadata
    • --skip-delete — skip deletion of old thumbnail files and full-size file
    • --porcelain — output only the attachment ID (useful for scripting)
  • composer.json — Registers media replace in extra.commands

  • features/media-replace.feature — Behat tests covering: local file, URL, porcelain mode, metadata preservation, error cases (invalid ID, missing file), and --skip-delete flag (asserts old file is preserved on disk)

Usage

# Replace with a local file
$ wp media replace 123 ~/new-image.jpg
Replaced file for attachment ID 123 with '/home/user/new-image.jpg'.
Success: Replaced 1 of 1 attachments.

# Replace with a remote URL, return ID only
$ wp media replace 123 'http://example.com/image.jpg' --porcelain
123

# Replace without deleting the old file
$ wp media replace 123 ~/new-image.jpg --skip-delete
Replaced file for attachment ID 123 with '/home/user/new-image.jpg'.
Success: Replaced 1 of 1 attachments.
Original prompt

This section details on the original issue you should resolve

<issue_title>Feature request: media replace subcommand</issue_title>
<issue_description>Hi,

I would like to suggest a new feature (subcommand) that would allow to seamlessly replace media file of existing attachment: media replace <file> <attachment_id>.

For the time being, I use Enable Media Replace whenever I need to replace media file, but it would be great to have a command line equivalent.</issue_description>

Comments on the Issue (you are @copilot in this section)


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

@github-actions github-actions bot added command:media Related to 'media' command help-wanted Extra attention is needed state:unconfirmed labels Mar 19, 2026
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add media replace subcommand for seamless media replacement Add wp media replace subcommand Mar 19, 2026
Copilot AI requested a review from swissspidy March 19, 2026 23:00
@codecov
Copy link

codecov bot commented Mar 19, 2026

Codecov Report

❌ Patch coverage is 75.34247% with 18 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Media_Command.php 75.34% 18 Missing ⚠️

📢 Thoughts on this report? Let us know!

This comment was marked as resolved.

swissspidy and others added 5 commits March 20, 2026 08:13
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ng, skip-delete test

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
@swissspidy swissspidy marked this pull request as ready for review March 20, 2026 08:04
@swissspidy swissspidy requested a review from a team as a code owner March 20, 2026 08:04
@swissspidy swissspidy added this to the 2.2.7 milestone Mar 20, 2026
@swissspidy swissspidy removed help-wanted Extra attention is needed state:unconfirmed labels Mar 21, 2026
@swissspidy swissspidy merged commit d33ec96 into main Mar 22, 2026
59 of 60 checks passed
@swissspidy swissspidy deleted the copilot/add-media-replace-subcommand branch March 22, 2026 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

command:media Related to 'media' command

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: media replace subcommand

3 participants