Block Editor: How to get title and post content within the WordPress admin UI in Javascript?
Adapting some old SEO module (which was written for TinyMCE) I have to access the current post title and post content via JavaScript. I have been avoiding Gutenberg in the past, but it seems like this will be no longer possible.
Within the old SEO module there are the following lines in the admin.js:
var title = $('#title').val().trim();
var content = $('#content').val().trim();
Those fields do not exist in Gutenberg anymore. I have tried to find the correct way to do this in the docs, but no luck so far. I have found:
wp.data.select("core/editor").getBlocks()
wp.data.select("core")
But both seem to be empty arrays in my case (although this post has content). I basically just need a way to get the textual contents of all blocks (and maybe seperatly from the main post title if this is possible). Who knows how to do that?
Leave an answer