Disabling WYSIWYG on comment field in Drupal 6
Integration of wysiwyg editors into Drupal continues to evolve and we can expect to see even more improvements in Drupal 7. In Drupal 6, the TinyMCE module was discontinued and the WYSIWYG module has taken its place. The WYSIWYG module works as an api for integrating editors such as TinyMCE, FCKeditor, markItUp, NicEdit and Whizzywig.
One important feature that WYSIWYG lacks is the ability to exclude certain form fields. We ran into this issue when we wanted to disable wysiwyg formatting on the comments field. There had been suggestions here and here on the drupal forums about setting a #wysiwyg attribute; this we tried with no success. Another suggestion was to use the Better Formats module, but the thought of adding yet another module for something so simple wasn’t very appealing.
In the end, we ended up writing a small amount of code to achieve this. We added the following function to our custom module. What we’re doing here is removing the ‘format’ entirely from the comments field. If you needed to alter a different form, replace comment-form with the id of your form and comment_filter with your field. You may need to try a print_r($form_id) to find out the id of your form item.
function my_module_form_alter($form_id, &$form) {
if ($form_id['#id'] == 'comment-form') {
$form_id['comment_filter']['format'] = array();
}
}

June 3rd, 2009 at 10:52 am
Thanks for the post, this helped a lot. Do you know why this works? From what I understand, all your doing here is removing the radio buttons for the format options from the form. Why does this also disable the TinyMCE editor?
Great fix.
Thanks,
~Matt
June 4th, 2009 at 3:52 pm
Hi Matt. Glad you found this post helpful.
Since the input formats are role-based in Drupal, you would normally see whichever ones your role is entitled to, on all textarea fields. The radio button options are built from the format array itself, which is why altering the form works. Replacing the format with an empty array causes Drupal to use the built-in format, as if the user has no input formats available at all, but only for whichever field and form you specify.
June 27th, 2009 at 1:10 am
I’m trying to get this to work by simply creating a dummy module for it. The contents of my module are here: http://drupalbin.com/10162 . What am I doing wrong? It’s still leaving WYSIWYG on all the comment forms
August 16th, 2009 at 2:18 am
I created a custom module named ‘commentform’ and put the function above into commentform.module
enabled the module.
Comment forms are still showing wysiwyg (tinyMCE)
I tried changing ‘my_module_form_alter’ to ‘commentform_form_alter’, disabled and re-enabled the module but that didn’t help either.
What am I missing?
Thanks
August 16th, 2009 at 6:49 pm
how do i create a custom module? can you upload the file(s) we need to disable the wysiwyg editor for comments?
cheers
August 17th, 2009 at 9:36 am
A question on the intergration of this function: where do you put this code exactly?
I pasted the function into page.tpl.php, but that’s not the way to do it, it seems, although the id of the comment field is indeed comment-form…
September 11th, 2009 at 9:37 am
Merakli, I’m not sure what might be missing from your module. Try outputting some test code in your module to make sure it’s loading correctly. Something like this would help you see if Drupal is getting into your function:
If that doesn’t work then your form may have a different id. Take a look at the page source to verify that. Also, watch those quotes if you’re copying and pasting from the browser.
September 11th, 2009 at 9:46 am
jaan and kurt:
I created a new module, which includes two files: my_module.info and my_module.module. The .info file needs the required fields described here. As for the actual code, the .module file contained the function written above. Make sure that your function name is derived from your module name.
For detailed instructions on module development, go here.
February 28th, 2010 at 10:33 pm
Can you PLEASE post an example module file? I tried my best, but still have that damn WYSIWYG there. I like it for writing posts, but HATE it for the comments due to security issues that can come up.
March 12th, 2010 at 5:41 am
Hi Noah,
Would you have to have seperate code for each page that your comment form would appear on? Or does the comment form have a universal ID that is the same on every page that it appears on?
Cheers, Garry.
March 25th, 2010 at 4:19 am
You can also use this Drupal module: http://drupal.org/project/better_formats
It’s much more simpler this way.
March 25th, 2010 at 12:09 pm
Kurt, you might try what I did in my theme… I didn’t have to make a whole new module for this. it doesn’t pass in $form_id .
In a php file in my theme, I have a function for theme_comment_form to suppress some field titles from printing before their textareas. It looks something like this:
function theme_comment_form($form) {
$form['comment_filter']['format'] = array();
}
April 23rd, 2010 at 6:16 am
Hi All,
Just replace the line text in comment.module in comment module
near about 1382 for me-
- $form['comment_filter']['format'] = filter_form($edit['format']);
+ $form['comment_filter']['format'] = array();
have a nice day and enjoy
no need to do any thing else
June 8th, 2010 at 6:53 am
If you have made a module, the code in the post didn’t work for me. Instead I used:
function MODULENAME_form_alter(&$form, $form_state, $form_id) {
if ($form_id == ‘comment_form’) {
$form['comment_filter']['format'] = array();
}
}
Andy
July 26th, 2010 at 4:35 am
The custom module code worked for me. Thanks!
August 4th, 2010 at 6:43 am
It works for me as well… Thanks mate
August 28th, 2010 at 4:10 am
Thank you, it work good for me to
September 11th, 2010 at 1:49 am
You’re awesome. Just wasted 2 hours trying to deal with tinymce and this was the trick. Cheers.
November 11th, 2010 at 6:38 pm
You could create a new input filter for your WYSIWYG that has limited input and then set that filter for use on your comments
January 17th, 2011 at 3:20 am
Reply number 13 worked for me. It was quick and easy. Thanks!
In comment.module file search for:
$form['comment_filter']['format'] = filter_form($edit['format']);
Then replace it with:
$form['comment_filter']['format'] = array();
Save the file and the issue should be resolved.
February 23rd, 2011 at 2:56 pm
Regarding replies #13 and #20: it is never a good idea to edit any part of Drupal core directly (including the “Core – optional” modules) being that your changes will be overwritten as soon as you do any updates to core. Also, if you run into a conflict between core and another module due to your altered code, it would be much easier to solve if your customized code was in a separate place.
Best practice is to make a small custom module as has been suggested above, or in some cases you can also integrate the code into the template.php file in your theme.
November 4th, 2011 at 2:22 am
Thanks Nitin Garg(Reply number 13), your suggestion worked for me.
November 28th, 2011 at 6:21 am
good work
December 2nd, 2011 at 10:59 am
setting the filter formats to an empty array is a bad idea for security reasons. It disabled the server-side filtering of submitted text.
Check here for a solution for those users using the wysiwyg module:
http://drupal.org/node/424164#comment-5316996