tinymce – Is there a way to grab the current value of the editor in Gutenburg/Classic WordPress editor? (Not the saved value of the post!)

Question

If someone stumbles upon same issue I found a way to achieve what I wanted (I hope someone is helped by this).

This is my solution: getEditedPostContent() contains the temporary post (modified) content:

let old_content="";
if ($('textarea#content').length >0) {
  old_content = $('textarea#content').html();
}
else {
  //"Gutenburg" style (>=WP 5.8)
  old_content = wp.data.select( 'core/editor' ).getCurrentPost().content; 
  if ( wp.data.select( 'core/editor' ).getEditedPostContent() ) {
    old_content =  wp.data.select( 'core/editor' ).getEditedPostContent();
  } 
}

And the actual replacement:

if ($('textarea#content').length >0) {
   $('textarea#content').html( new_content );
}
else {
  //"Gutenburg" style (>=WP 5.8)
  wp.data.dispatch( 'core/block-editor' ).resetBlocks( wp.blocks.parse( new_content ) );
}

0
bestprogrammerintheworld 2 months 2021-07-26T08:41:32-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse