Support » Developing with WordPress » Adding a Gutenberg block programmatically

  • Resolved Tom Geller

    (@tgeller)


    Beginning WordPress developer here. I’m working with a legacy database with lots of information in custom fields (originally created with ACF). Among them are videos I’d like to put in a Gutenberg “Embed” block, e.g. from YouTube.

    I’ve created a custom theme (child of twentytwenty). I’ve made other custom fields appear (as plain text) by adding them to template-parts/content.php, below “the_content”.

    My plan has been to add code like, “If there’s data in the wpcf-embedded-media field, add an Embed block to the page and stick the data in there.” But I can’t figure out the “add an Embed block to the page” part.

    Of course I’d welcome suggestions for other ways to do this. Thanks in advance for your help!

    • This topic was modified 3 years, 9 months ago by Tom Geller.
    • This topic was modified 3 years, 9 months ago by Tom Geller.
    • This topic was modified 3 years, 9 months ago by Tom Geller.
Viewing 2 replies - 1 through 2 (of 2 total)
  • You’re thinking of it a little skewed. The “block” part is for the user interface in the editor. That block markup gets rendered before the theme gets the_content. To add to what the theme is outputting, you simply need to filter the appropriate thing. For legacy content, that means getting the post meta fields and adding them to the_content.
    Or you can manipulate your database, so that it’s not in post meta anymore, but directly in the content. Embeds work with no extra markup needed – simply the URL on a line by itself.

    Thread Starter Tom Geller

    (@tgeller)

    Thank you so much! You’ve cleared up a few things for me. I was just sitting down to work on this; now I have much better focus.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding a Gutenberg block programmatically’ is closed to new replies.