Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gutenberg editor content in JavaScript (WordPress)

Back in TMCE days, we could easily get editor content with editor.getContent(). However in new Gutenberg editor, I can't find a method to do that.

I need all editor content as HTML (the way it will be saved in database).

I found wp.block.serialize() method which sounds promising. But seems to need blocks (as a parameter). So I'm kind of stuck.

like image 783
shramee Avatar asked Jan 11 '18 12:01

shramee


1 Answers

As of version 3.1. of Gutenberg, try this:

to get the plain block content:

var originalContent = wp.data.select( "core/editor" ).getCurrentPost().content;
var editedContent = wp.data.select( "core/editor" ).getEditedPostContent();

to render the post (transform to blocks):

wp.blocks.parse( editedContent );
like image 59
Flow Avatar answered Oct 05 '22 19:10

Flow