Replace attachment file keeping same id
const url = 'https://example.com/wp-json/wp2/enable-media-replace/v1/attachments/1/replace';const form = new FormData();form.append('file', 'file');form.append('mode', 'replace');form.append('time_mode', 'update_all');form.append('custom_date', 'example');form.append('new_filename', 'example');form.append('location_dir', 'example');
const options = {method: 'POST', headers: {'X-WP-Nonce': '<X-WP-Nonce>'}};
options.body = form;
try { const response = await fetch(url, options); const data = await response.json(); console.log(data);} catch (error) { console.error(error);}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=exampleReplaces 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.
Authorizations
Section titled “Authorizations”Parameters
Section titled “Parameters”Path Parameters
Section titled “Path Parameters”Numeric entity id.
Request Body
Section titled “Request Body”object
Uploaded file (multipart).
Mode (connection, replace, or pack).
Field “time_mode” as returned or accepted by PHP.
Field “custom_date” as returned or accepted by PHP.
Field “new_filename” as returned or accepted by PHP.
Field “location_dir” as returned or accepted by PHP.
Responses
Section titled “Responses”object
Media library attachment ID.
URL.
Mode (connection, replace, or pack).
Field “filename” as returned or accepted by PHP.
Field “mime_type” as returned or accepted by PHP.
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.
object
Machine-readable error or event code.
Message text (log, PIN, or password reset).
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.
object
Machine-readable error or event code.
Message text (log, PIN, or password reset).
Extra WP_Error or license payload.
object
Examplegenerated
{ "code": "example", "message": "example", "data": {}}