Support » Developing with WordPress » How to get html code of “core/image” using API?

  • I need to get the full html code of the “core/image” block, knowing only its attributes (media ID, size, etc). Is there an rest-API for this? The block-renderer does not seem to work with the “core/image”. Perhaps there is an appropriate PHP function? Then I would be able to create the desired rest-endpoint on my own.

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


    The block HTML is only contained within the post content. With the media ID you can get all the attachment data through the API /media/ path which includes rendered HTML, but that is not necessarily the HTML used by a specific block. For example, caption elements are not included in media data. The only way to get that is through the post content.

    Thread Starter Alex Kozack


    Actually, I need it to programmatically create post content. I have a text. I have a uploaded image, have it ID. I need to combine them and create a post. All of this should happen programmatically in the our corporate utility.

    Before WordPress v5 it was real. I used description.renderer in attachment data and insert into the post.

    I can still do that. However, to make it easier for our employees, the final text must be saved as gutenberg-blocks.

    I need at least some way to get the html block code by its name and attributes. Anyone, on the server or on the client.

    As I would do it manually, in a visual editor – I chose a block, inserted the necessary parameters into it. But programmatically.

    Moderator bcworkz


    You can get all the attachment data needed from the /media/ API route. You can use PHP to assemble post content that is identical to that created by the block editor. Examine existing editor content to discern the appropriate structure. If no captions are involved you might use the “rendered” content field as the HTML within the block comment delimiters. It may vary depending whether the image should link to an attachment page or media file or not anything. In any case, appropriate HTML can be constructed with PHP from media data.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to get html code of “core/image” using API?’ is closed to new replies.