Skip to content

Replace attachment file keeping same id

POST
/wp-json/wp2/enable-media-replace/v1/attachments/{id}/replace
curl --request POST \
--url https://example.com/wp-json/wp2/enable-media-replace/v1/attachments/1/replace \
--header 'Content-Type: multipart/form-data' \
--header 'X-WP-Nonce: <X-WP-Nonce>' \
--form file=@file \
--form mode=replace \
--form time_mode=update_all \
--form custom_date=example \
--form new_filename=example \
--form location_dir=example

Replaces the attachment file bytes while keeping the media-library ID. If media-playback artifacts exist, returns 409 — clear them first. Multipart file plus replace mode.

id
required
integer

Numeric entity id.

Media typemultipart/form-data
object
file
required

Uploaded file (multipart).

string format: binary
mode

Mode (connection, replace, or pack).

string
Allowed values: replace replace_and_search
time_mode

Field “time_mode” as returned or accepted by PHP.

string
Allowed values: update_all update_modified custom
custom_date

Field “custom_date” as returned or accepted by PHP.

string
new_filename

Field “new_filename” as returned or accepted by PHP.

string
location_dir

Field “location_dir” as returned or accepted by PHP.

string
Media typeapplication/json
object
attachment_id

Media library attachment ID.

integer
url

URL.

string
mode

Mode (connection, replace, or pack).

string
filename

Field “filename” as returned or accepted by PHP.

string
mime_type

Field “mime_type” as returned or accepted by PHP.

string
Examplegenerated
{
"attachment_id": 1,
"url": "example",
"mode": "example",
"filename": "example",
"mime_type": "example"
}

Not authenticated. WordPress REST: rest_not_logged_in / rest_forbidden. Use cookie + X-WP-Nonce or Application Password.

Media typeapplication/json
object
code

Machine-readable error or event code.

string
message

Message text (log, PIN, or password reset).

string
data

Extra WP_Error or license payload.

object
Examplegenerated
{
"code": "example",
"message": "example",
"data": {}
}

Insufficient capability or invalid REST nonce. WordPress REST: rest_forbidden / rest_cookie_invalid_nonce.

Media typeapplication/json
object
code

Machine-readable error or event code.

string
message

Message text (log, PIN, or password reset).

string
data

Extra WP_Error or license payload.

object
Examplegenerated
{
"code": "example",
"message": "example",
"data": {}
}