Convert images to WebP and swap references

MediaAdvanced~30 min

Before you start

A connected site with media reads, the upload ability, content reads, and the replace-in-post write all enabled. Take a database backup before a run, since this touches both the library and post content.

WebP images are usually a good deal smaller than the JPEG or PNG they replace, which is why people want to convert a page full of heavy photos. The honest version of this job with a governed agent is not a magic optimizer button. The agent re-encodes each image you point it at, uploads the result as a new WebP attachment, and then updates the places in your content that referenced the old file. It is deliberate and batch by batch, and you approve the swaps before any post changes.

Because this both adds to the library and edits post bodies, it earns the two-pass, wait-for-approval flow.

The prompt
I want to convert [the images in these three posts] to WebP and update the
posts to use them. Be clear with me about scope: I know this re-encodes and
re-uploads each image and swaps the reference, it is not a server-side bulk
optimizer of my existing files.
First pass, change nothing that I have not approved: list the images in scope,
tell me which are worth converting, and for each planned swap show me the old
URL and the post it appears in. Then re-encode those images to WebP and upload
each as a new attachment.
Only after I approve a batch, run the find-and-replace on those post bodies to
point them at the new WebP, in small batches, showing me each batch before it
saves. Do not delete the original attachments. At the end, give me the new
attachment ids and the list of every post you edited.

Why this is safe to run

The planning is all reads and additive uploads. Listing the images and finding the posts that reference them writes nothing to your content, and uploading a new WebP adds an attachment without altering the original. So you get to see every planned reference swap before a post body changes.

The one ability that edits your content is a literal find-and-replace inside a post’s body. It never changes a post’s status, never publishes or unpublishes, and its edits are reversible through the post’s revisions. The upload ability takes base64 image data and accepts jpg, png, gif, and webp while rejecting SVG, so the agent cannot slip an unexpected file type into the library. Every ability here stays off until you enable it, each call is checked against the capability of the user you bound the agent to, and every upload and every post edit lands in the Activity Log. If a swap looks wrong, the log points you at the exact post, and the revision lets you roll it back.

Already connected? You do not have to paste this at all. Ask your agent for the “convert images to WebP and swap references” recipe, describe the set, and it runs the same approve-first flow on your own site.

How it works

  1. List the images you named and pick the ones worth re-encoding to WebP.
  2. For each one, the agent re-encodes the image and uploads the WebP as a new attachment through the upload ability, which takes base64 image data.
  3. Find the posts that reference the old image URL through a content read.
  4. Show you the planned swaps for a small batch and wait for approval before editing any post.
  5. Run the approved find-and-replace on post bodies in small batches, pausing to show each batch.
  6. Return the new attachment ids and every post it edited so you can review it.

Questions

Is this a one-click bulk image optimizer?
No, and it is important to be clear about that. There is no server-side optimizer here. The agent re-encodes each image it was given, uploads the WebP as a new attachment, and then swaps the reference in your content. It works image by image on the set you approve, not as a background sweep of the whole uploads folder.
What happens to the original images?
They stay in the library. This recipe uploads new WebP attachments and repoints your content at them; it does not delete the originals. Removing the old files is a separate, destructive step you would scope on purpose after you have confirmed the swaps look right.