nicedevman
nicedevman

Reputation: 37

WYSIWYG Editor in custom Wordpress Widget

Replacing a textarea with WordPress TinyMCE wp_editor()

I've followed the above guide, but the WYSIWYG has some issues.

$text = str_replace(array("\r", "\n"), '', $instance['text']);

$settings = array( 'textarea_name' => $this->get_field_name('text') );
wp_editor( esc_attr( $text ), 'text', $settings );

The result is this:

result

The tab wont switch (sometimes Text is selected initially, sometimes Visual). The Add Media button appears to work, but when clicking Insert into Post, the modal disappears and nothing happens.

There's nothing in the console, so I'm not sure what's going wrong. Might someone know what is happening, or perhaps what direction to move in order to start figuring it out?

Upvotes: 1

Views: 3770

Answers (1)

VZimmerl
VZimmerl

Reputation: 175

Maybe a little too late but i saw the post a few minutes ago and had the same issue.

I tried the examples from the Wordpress Code Reference:https://developer.wordpress.org/reference/functions/wp_editor/

So you don´t need $settings for initializing the editor, just use it like this:

wp_editor( $content, $editor_id );

For usage in a Widget:

public function form( $instance ) {
    $text = $instance['text'];
    wp_editor(esc_attr( $text ),  $this->get_field_id( 'text' ));
}

And there is another catch. You need a Unique ID for the editor as described here: https://wordpress.stackexchange.com/questions/82670/why-cant-wp-editor-be-used-in-a-custom-widget

Hope this helps!

Upvotes: 2

Related Questions