0

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?

1

If we do this in the browser dev tools console:

var b = wp.data.select("core/editor");

We can then inspect the b variable to see what functions it exposes via autocomplete, or debugging tools.

Notice, I didn't call getBlocks(), the post title has never been a part of the post content, why would that change now?

A look around gave me this a.getCurrentPost(), which returns a post object with a post title, but this will be the original post title, not the current post title, and doesn't get updated as the user edits the title

A quick google however gave an identical Question with an indepth answer on stackoverflow: https://stackoverflow.com/questions/51674293/use-page-title-in-gutenberg-custom-banner-block

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.