Within your widget, you can include a editor with a textarea and the class custom-widget-wp-editor
.
<textarea id="<?php echo $this->get_field_id( 'header' ); ?>" name="<?php echo $this->get_field_name( 'header' ); ?>" class="custom-widget-wp-editor"><?php echo $header; ?></textarea>
Make sure you include the JS-file within the widgets and customizer view.
/**
* Add a fix for tinymce within widgets.
*
* @return void
*/
function custom_widgets_widget_editor_script() {
global $pagenow;
if ( 'widgets.php' === $pagenow || 'customize.php' === $pagenow ) {
wp_enqueue_editor();
wp_enqueue_script( 'wp-editor-widgets', get_stylesheet_directory_uri() . '/js/wp-editor-widgets.js', array( 'jquery' ), '1.0', false );
}
}
add_action( 'admin_enqueue_scripts', 'custom_widgets_widget_editor_script' );
Hi @jeffreyvr,
thanks for the code, I managed to get it work, and it helps me a lot to achieve what I want in my widget!
However live update doesn't work for me in customize.php window, and the editor value also does not get submitted on widget saving, because there is no Save button for widgets in this window, which you are targeting with the
// To prevent the editor from not submitting the value, we click the switch html tab.
part of the code.I found a solution for this by adding an event listener to the wp.editor.initialize function:
Notes:
// To prevent the editor from not submitting the value, we click the switch html tab.
part of your code is not needed, because this methods works both in cutomize.php and widgets.php window.