Skip to content

Add wp media replace subcommand#240

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/add-media-replace-subcommand
Draft

Add wp media replace subcommand#240
Copilot wants to merge 2 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()
    • Deletes old thumbnails, updates MIME type, refreshes _wp_attached_file meta, and regenerates attachment metadata
    • --skip-delete — skip deletion of old thumbnail files
    • --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, and error cases (invalid ID, missing file)

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 images.

# Replace with a remote URL, return ID only
$ wp media replace 123 'http://example.com/image.jpg' --porcelain
123
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 81.13208% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Media_Command.php 81.13% 10 Missing ⚠️

📢 Thoughts on this report? Let us know!

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 help-wanted Extra attention is needed state:unconfirmed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: media replace subcommand

2 participants