Getting and modifying Gutenberg post content with Javascript
-
Hello,
I’ve been hitting my head on walls for a few weeks now, trying to modify post content with a plugin.
Context : translation plugin. Gets title, excerpt and content, translates via API and update title, excerpt, content with the resulting strings.
Translation do no touch <– wp xxxx –> tags (they are in the source and the result HTML code)Title is simple :
wp.data.dispatch( 'core/editor' ).editPost( { title: new_title} )
Excerpt is simple :
wp.data.dispatch( 'core/editor' ).editPost( { excerpt: new_excerpt } )
Post content is a major pain.
I wish it was :
wp.data.dispatch('core/editor').editPost( {content: new_content });
but that doesn’t work due to blocks getting in the way.
Right now, I’m exploding the translated post content into more or less blocks.
Using :var newBlock = wp.blocks.createBlock( "core/paragraph", { content: content}); var inserted = wp.data.dispatch( "core/block-editor" ).insertBlocks( newBlock );
works more or less with paragraph blocks, but derails with image/heading blocks.
Is there a way to just dump the new post content into the current editor ?
If not, is there a recommended way to parse content with <– blocks –> and insert them into the editor ?Thanks for your help
- The topic ‘Getting and modifying Gutenberg post content with Javascript’ is closed to new replies.