Call tinyMCE.triggerSave() before sending metabox data
Prior to sending metabox data for save, we need to call window.tinyMCE.triggerSave() to update any <textarea> fields controlled by wp_editor() instances.
If TinyMCE is used on a <textarea> field, then the data doesn't end up in the actual <textarea> until a save is triggered.
Originally #6293
also currently experiencing this problem.
workaround:
document.addEventListener( 'DOMContentLoaded', () => {
wp.editor.initialize( 'field_name', {
'textarea_name': 'field_name',
'tinymce': {
'init_instance_callback': ( editor ) => {
editor.on( 'change', function() {
const editor = tinymce.get( this.id );
document.getElementById( 'field_name' ).value = editor.getContent();
} );
},
},
} );
} );
where field_name is the id= and name= of the textarea being 'enhanced.'
@krisgale Can you share a code snippet and testing instructions to reproduce your specific problem? I'd like to verify the change in #8762
no code snippet as such (already replaced with above workaround), but adding any metabox that contains an instance of wp_editor would replicate the issue... disable gutenberg, content saves; enable gutenberg, content does not save.
also, #8762 mentions ACF, which i'm not using.
I put together a code snippet:
add_action( 'add_meta_boxes', function(){
add_meta_box( 'test_tinymce', 'Test TinyMCE', function( $post ){
$field_value = get_post_meta( $post->ID, 'test_tinymce', true );
wp_editor( $field_value, 'test_tinymce_id', array(
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'test_tinymce',
'textarea_rows' => 10,
'teeny' => true
) );
}, null, 'advanced', 'high' );
});
add_action( 'save_post', function( $post_id ){
if ( ! isset( $_POST['test_tinymce'] ) ) {
return;
}
update_post_meta( $post_id, 'test_tinymce', $_POST['test_tinymce'] );
});
See conversation in https://github.com/WordPress/gutenberg/pull/8762#issuecomment-412137005 for the failed attempt at fixing the issue.
@danielbachhuber thanks for staying on top of it
So the current state is that Gutenberg will not save content from wp_editor() fields in metaboxes, correct?
@krisgale Unfortunately I can't get your workaround to correctly work.
field_name should be the $editor_id parameter you set on wp_editor() corrrect?
So my code would look like this:
document.addEventListener( 'DOMContentLoaded', () => {
wp.editor.initialize( '_wp_editor_eve_post_links', {
'textarea_name': '_wp_editor_eve_post_links',
'tinymce': {
'init_instance_callback': ( editor ) => {
editor.on( 'change', function() {
const editor = tinymce.get( this.id );
document.getElementById( '_wp_editor_eve_post_links' ).value = editor.getContent();
} );
},
},
} );
} );
But the change event does not get triggered.
important note per the above ^
in implementing this workaround, i replaced the call in php to wp_editor() with only js to instantiate the tinymce instance
@danielbachhuber has this bug been fixed with a change in Version 3.9 or 3.8? (I think with 3.7 the issue has existed)
Because after the newest update it seams to work now.
@michakrapp I'm not sure whether the issue has been fixed accidentally. When we worked on this last, we weren't able to reproduce the issue reliably: https://github.com/WordPress/gutenberg/pull/8762#issuecomment-412137005
Strange thing - after the update it has been working a few times. Now the error is back.
But I can confirm that call window.tinyMCE.triggerSave() before saving works like you demonstrated in #8762
Hello,
Our users experience a similar problem. They're using Meta Box plugin to create repeatable editors. The data is not saved correctly. Details are here.
I tested and see that when the editor in the Visual mode, the content is not saved. When it's in Text mode, it seems to work fine.
I tried calling window.tinyMCE.triggerSave() before saving, and it works. So I'd suggest merging the PR #8762.
Thanks
I am able to recreate this issue with our memberlite-elements plugin. It adds a few wp_editor instances into the meta boxes below the page. When I click to update the page, the meta box fields aren't saved. When I debugged, it's because the $_POST['field_name'] value was not updated. It seems that TinyMCE would update this before the post got saved and the suggestion solution here would work.
I tried typing window.tinyMCE.triggerSave() into the JS console and then saving and that doesn't work for me. Something else is needed.
I am using WP 5.0-beta3-43878, Gutenberg Version 4.2.0, Chrome
FWIW here is where we insert our editor: https://github.com/strangerstudios/memberlite-elements/blob/development/elements/page_banners.php#L73
Here is where we process it. It fails on this line since the POST content is empty. https://github.com/strangerstudios/memberlite-elements/blob/development/elements/page_banners.php#L184
Note that this plugin is meant to work with the Memberlite theme, but does still work (in terms of saving values) with Twenty* themes and fails with a Twenty* theme active.
Do we need a better minimal example? Do we need someone to dig into this further and look for the fix? Let me know. I might have time.
Wondering if this will be fixed in WP Core. Currently, any meta boxes with wysiwyg editors don't save in the block editor, and the promised meta box compatibility flags do not work either: meta boxes marked as incompatible are simply skipped for output, rather than falling back to the classic editor.
@robincornett I suspect that this will be fixed in WP Core when the issue is understood fully. In my view this is related to TinyMCE not loading correctly for Metaboxes. That is a developer concern. For users, however, it may be helpful to know that Metaboxes will save correctly and fully if done using the Text tab, not Visual. That is, if you make all of the changes that you wish using the Visual tab, ie using the TinyMCE editor, and you switch to the Text tab, meaning you will see the text rendering of the html markup, when you click Publish or Update, the data in the metaboxes, including the markup, will save. Because of I am confident that this will be corrected in Core, but it will take someone who understands the waterfall of javascript in order to place the .triggerSave() where it will be recognized and the change effectuated.
Thanks, @pbrocks--I get that it will theoretically be fixed in Core, but at the moment, I'm blocked on updating a plugin because of this issue (telling users to use the text tab isn't a great option for me) combined with no backwards compatibility in place yet. I hate to tell my users that they have to add the Classic Editor plugin just to keep using the plugin they've been using for an extended period of time, but right now that is all I can do. My concern is as a developer, not as a user.
Having the same issue here. Can we hope this will get fixed for WordPress 5.0?
I can't imagine telling users to make changes in "Visual" tab, while switching to "Text" tab before saving/updating post so their changes are properly saved... That's is a very bad UX.
According to the assigned milestone, it doesn't look that it's going to be fixed in WordPress 5.0 nor 5.0.1 which is a bummer.
I'm still interested in fixing this but last time I tried, it was not reproducible consistently which made the fix useless. If someone could provide me with a minimal metabox with the issue, I'd be happy to try to fix it again.