• Hi,

    We have an app we serve data from our WordPress website to via the REST API. This works great but we noticed recently that grabbing post images pulls in all of the different image sizes together which slows down post loading times.

    Is it possible to create a custom endpoint that gets the same data as /wp/v2/media/(?P<id>[\d]+) but filters for the specific image size such as: /wp/v2/media/(?P<id>[\d]+)/(?S<size>[\d]+)/?

    This would be ideal as we could then pull in just the size we want but still get all the rest of the data such as the image id, creation date etc.

    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You can always add a custom route/endpoint to do anything that’s feasible to do with PHP.

    It may not be necessary though. I believe you can add additional API query vars to existing endpoints even if they are not formally defined. If true, you could modify the query args used to get image sizes so that only the requested size is returned. I’m unsure of the specifics, but I am reasonably confident this is possible. You’ll need to dig into the involved source code to see what opportunities there are in the available filter and action hooks.

    Thread Starter Ibby

    (@ibby)

    Hi @bcworkz!

    Thank you very much for the reply. I am going to attempt this today though any further info you may have which could push me in the right direction would be very helpful.

    The docs are very scant on details….

    Moderator bcworkz

    (@bcworkz)

    Which suggestion are you attempting? Adding “size” query var to /media/ endpoint or developing a new, custom route/endpoint?

    By the time you see this your attempts may be beyond anything I can suggest. If so I hope it hasn’t been too painful ?? But if you’re still not very far along maybe I can still help with more information.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filter Image Sizes via WP REST’ is closed to new replies.