$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Import external data into post using custom Gutenberg block|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Import external data into post using custom Gutenberg block

matteradmin9PV0评论

I've created a simple Gutenberg block that displays the transcript of an externally-hosted video.

When creating a new WP post, the author enters the ID of a video in the block's editor section. This triggers my custom PHP to fetch and parse the video's caption file via a 3rd-party API. WP's ServerSideRender is then used to render the caption file in both the editor view and the front-end view. This works very well.

The problem is that the transcript is never truly imported or saved in the WP database in any fashion -- the transcript is always re-fetched in real-time, every time the page is visited by a viewer. The transcript is never persisted in the WP database, which means it is not searchable by site visitors.

My goal is to enable the WP author to fetch the video's transcript when creating a post by entering the video ID into a Gutenberg block attribute field. Once fetched, the transcript would be automatically inserted into the WP post's body as editable HTML so it can be cleaned up by the author (fix typos and punctuation), then persisted in the WP database.

I've spent many many hours trying to sort this out and have had no luck. My Google-Fu is failing me, in part because Gutenberg is so new.

So... I'm already fetching the transcript via the API, I'm just at a loss how to get it out of Gutenberg's React rendering system. How can I insert the retrieved text into the post body as HTML instead of rendering as uneditable text in a Gutenberg block?

Thanks!

I've created a simple Gutenberg block that displays the transcript of an externally-hosted video.

When creating a new WP post, the author enters the ID of a video in the block's editor section. This triggers my custom PHP to fetch and parse the video's caption file via a 3rd-party API. WP's ServerSideRender is then used to render the caption file in both the editor view and the front-end view. This works very well.

The problem is that the transcript is never truly imported or saved in the WP database in any fashion -- the transcript is always re-fetched in real-time, every time the page is visited by a viewer. The transcript is never persisted in the WP database, which means it is not searchable by site visitors.

My goal is to enable the WP author to fetch the video's transcript when creating a post by entering the video ID into a Gutenberg block attribute field. Once fetched, the transcript would be automatically inserted into the WP post's body as editable HTML so it can be cleaned up by the author (fix typos and punctuation), then persisted in the WP database.

I've spent many many hours trying to sort this out and have had no luck. My Google-Fu is failing me, in part because Gutenberg is so new.

So... I'm already fetching the transcript via the API, I'm just at a loss how to get it out of Gutenberg's React rendering system. How can I insert the retrieved text into the post body as HTML instead of rendering as uneditable text in a Gutenberg block?

Thanks!

Share asked Feb 22, 2019 at 10:07 pipwerkspipwerks 1135 bronze badges
 | 

1 Answer 1

Reset to default 1

You can save your data into the database by using save method in registerBlockType. Just make requests on ComponentDidMount via using JS fetch API, store and use it in edit method. In the end use save method instead of using Server Side Rendering.

Server side rendering should only be use if your content changes like latest posts. If this is just a one time data then it's not needed.

Post a comment

comment list (0)

  1. No comments so far