Set a featured image from a URL

MediaIntermediate~10 min

Before you start

A connected site with the upload-media and set-featured-image write abilities enabled. The image must be a jpg, png, gif, or webp; SVG is rejected.

You found the right image for a post, and it is sitting at some URL. The manual version is download, rename, upload through the media library, then open the post and set the featured image. Four steps and a browser tab shuffle for one picture.

Here the agent does the fetching and the busywork, and you keep the one decision that matters: yes, use this image on this post. It is a write, so it waits for that yes before anything lands.

The prompt
Set the featured image on [post ID 142] from this image URL:
[https://example.com/photo.jpg]. Fetch the image yourself and read it
as base64, since the upload takes image data, not a link. It has to be
a jpg, png, gif, or webp. Before you upload, show me the source URL,
the file format, and the post it will go on, and wait for my go-ahead.
Once I approve, upload it to the media library and set it as that
post's featured image, then tell me the new attachment ID.

Why this is safe to run

There are two writes here and both are narrow. The upload adds one new image to the media library, and setting the featured image points one post at that attachment. Neither touches the post body, its status, or anything already in your library.

The format rule does real work. Because upload-media takes base64 and accepts only jpg, png, gif, and webp, the agent has to fetch the URL and convert it before the plugin will look at it, and the plugin validates the bytes it receives rather than trusting a link. An SVG or an unrecognized format is rejected outright, so nothing questionable gets stored. On top of that, the write abilities stay off until you enable them, every call is checked against the capability of the user you bound the agent to, and the upload and the featured-image change both land in the Activity Log. If you set the wrong image, you change the featured image back, and the post copy is never affected.

Already connected? You do not have to paste this. Ask your agent for the “set a featured image from a URL” recipe, or just point it at an image and a post, and it runs the same fetch, upload, and approval flow on your own site.

How it works

  1. Give the agent the image URL and the post you want it on.
  2. The agent fetches the image and reads it as base64 for upload.
  3. It shows you the source, the format, and the target post, and waits.
  4. On your go-ahead, it uploads the image to the media library.
  5. It sets that new attachment as the post's featured image.

Questions

Why does the agent handle the URL and not the plugin?
upload-media takes the image as base64 data, not a link. So the agent is the one that fetches the URL and converts it, then hands the plugin the bytes. The plugin only ever receives an image it can validate as a real jpg, png, gif, or webp.
What if I pass an SVG or an odd format?
The upload is rejected. upload-media accepts only jpg, png, gif, and webp, so anything else stops at the door and nothing is written.