I’ve been working on developing a plugin that adds a widget that allows you to edit the content with a wysiwyg editor. I want to use the default wordpress editor by calling wp_editor()
. I’ve seen people say you can’t do this, and sure enough, it crashes when you try to update the widget content.
Can anyone explain why wp_editor()
can’t be used in widgets?
I’m hoping to figure out how to get around that once I know why it doesn’t work. However, Chrome doesn’t spit out any errors in the console, so I’m not sure what’s happening that it bugs out.
Thanks,
goldentoa11
Short answer: Because there is a hidden widget where the TinyMCE appears first.
Long answer (sorry, a very long answer):
Go to the Codex and copy the example widget
Foo_Widget
to make sure we are talking about the same code. Now open your IDE (not an editor) and write a short testing widget as plugin. Starting with a minimal plugin header…… adding the widget code from the codex and registering the widget with the
add_action()
call. Now modify theform
method within the widget class as shown here:Go to your blog, activate the plugin, go to the widget page and drag the
Foo Widget
into a sidebar. You will see it will fail.Do you see the
Foo Widget
description on the left side? Do a right click on the description and choose ‘Inspect’ from your developer tools (you have some developer tools like FireBug or Chrome DevTools, huh? In FireFox you can also choose the build in ‘Inspect Element’). Browse through the HTML code and you will see there is an editor wrap inside the ‘hidden’ widget.You can see one editor in the sidebar on the right and there is an editor in the ‘hidden’ widget on the left. This cannot work properly because TinyMCE don’t know which editor should be used.
What do we need?
We need a unique ID for our editor.
Modify the code within the
form
method again with the code above. We create a unique number withrand()
and append this number to theid
andname
attributes. But stop, what about saving the values???Go to the
update
method and adddie(var_dump($new_instance));
in the first line. If you pressSave
on the widget, the script will die and print out the submitted values. You will see there is a value likewp_editor_528
. If you reload the page and save the widget again, the number will be changed to something else because it is a random number.How will I know which random number was set in the widget? Simply send the random number with the widget data. Add this to the
form
methodIn the update routine, we can now access the random number with
$new_instance['the_random_number'];
and so we can access the editor content withAs you can see, the editor content will be submitted and you can save it or do anything else with it.
Caveat
The editor content is only submitted correctly if the TinyMCE is not in visual mode (WYSIWYG mode). You have to switch to the text mode befor press ‘Save’. Otherwise the predefined content (in this case
$content = 'Hello World!';
) is submitted. I don’t now why, but there is a simple workaround for this.Write a small jQuery script that triggers the click on the ‘text’ tab before saving the widget content.
JavaScript (saved as
widget_script.js
somewhere in your theme/plugin folders):And enqueue it
That’s it. Easy, isn’t it? 😉
do not know the exact answer why wp_editor() is not working inside of widgets, but widgets have some restrictions.
But wp_editor is only one of many possibilities to use the visual editor in the backend of WordPress. Try to initialize the visual editor with jQuery somehow. I do not think that you can do it inside the widget window itself, but a lightbox or something like that might help.
Here is a way to do it:
It’s quite possible to accomplish tinyMCE in widget. In fact it has been already done. The reason why simply calling
wp_editor()
doesn’t work in widget form is that form markup is asynchronously delivered using AJAX. Try searching here for more details.