Skip to content

Instantly share code, notes, and snippets.

@goldenapples
Created May 26, 2012 20:19
Show Gist options
  • Save goldenapples/2795190 to your computer and use it in GitHub Desktop.
Save goldenapples/2795190 to your computer and use it in GitHub Desktop.
Rich text editors for Gravity Forms
<?php
/*
Plugin Name: Rich Text editors for Gravity Forms
Description: Converts the textarea fields in Gravity Forms to WordPress rich text editors
Author: Nathaniel Taintor
Author URI: http://goldenapplesdesign.com
Version: 1.0
License: GPLv2
*/
/*
* Gravity Forms filter to allow the post body and textarea inputs to use the wp_editor
* visual editor. To use wp_editor instead of default textarea, just set css class
* to "richtext"
*
* @uses wp_editor() Requires WordPres >= 3.3.0 for this function
*
* see http://codex.wordpress.org/Function_Reference/wp_editor for arguments to
* pass to the wp_editor() function. At the very minimum, you probably will need
* to pass some styles in 'editor_css'; otherwise the gravityforms styles and your
* theme styles will likely override some of the styles applied to the editor
* buttons and text.
*
*/
add_action( 'gform_field_input', 'gforms_wp_editor', 10, 5 );
function gforms_wp_editor( $input, $field, $value, $lead_id, $form_id ) {
if( $field["cssClass"] == 'richtext' ) {
ob_start();
wp_editor( $value, "input_{$form_id}_{$field['id']}",
array(
'media_buttons' => false,
'textarea_name' => "input_{$field['id']}"
) );
$input = ob_get_clean();
}
return $input;
}
@goldenapples
Copy link
Author

There's a known issue with the TinyMCE class used in wp_editor(); that it can't be moved around in the DOM: http://codex.wordpress.org/Function_Reference/wp_editor#Notes

So I'm thinking that this approach will not work well if you're trying to use an Ajax-enabled Gravity Form.

I haven't tested it or played around with it, but the only fix I could imagine would be somehow registering a javascript handler on the "gform_page_loaded" hook that calls whatever functions are needed to initialize the tinymce editor. tinyMCE.init() would be a good place to start, but I think there might be more function calls necessary to fully initialize an editor.

@biju001
Copy link

biju001 commented Jan 14, 2015

i paste your code in my function. but now i don't see any editor add any text editor

@EvanHerman
Copy link

This doesn't seem to save any formatting within the WYSIWYG. It's all stripped on submitting.

@bozzmedia
Copy link

Rad! Code largely works on current Gravity Forms, I have run into an issue with pasted text:

I can include rich text if I manually type out content into the post content field, however if I paste in content directly, it looks fine and is formatted properly before submitting the form, but the output results in completely blank content (not just no styles, no content at all). I can then go in the admin area and edit the post, no content in the post body.

Anyone have any idea what might be stripping cut and pastes like that? Thanks!

@lekiend
Copy link

lekiend commented May 10, 2023

Hello,
I would change the test with the following:
if( $field["useRichTextEditor"] == '1' ) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment