diff --git a/gravity-forms/gw-rich-text-html-fields.php b/gravity-forms/gw-rich-text-html-fields.php index 2097837c1..db8edde32 100644 --- a/gravity-forms/gw-rich-text-html-fields.php +++ b/gravity-forms/gw-rich-text-html-fields.php @@ -16,6 +16,7 @@ add_action( 'admin_init', function() { if ( GFForms::get_page() === 'form_editor' ) { wp_enqueue_editor(); + wp_enqueue_media(); // Required for media uploads } } ); @@ -70,7 +71,35 @@ jQuery( document).on( 'tinymce-editor-setup', function ( event, editor ) { var editorId = 'field_rich_content'; if ( editor.id === editorId ) { - editor.settings.toolbar1 = 'bold,italic,underline,bullist,numlist,alignleft,aligncenter,alignright,link'; + editor.settings.toolbar1 = 'bold,italic,underline,bullist,numlist,alignleft,aligncenter,alignright,link,image'; + + // Handle image insertion from media library + editor.addButton( 'image', { + icon: 'image', + tooltip: 'Insert Image', + onclick: function() { + var frame = wp.media({ + title: 'Insert Media', + button: { text: 'Insert into HTML Field' }, + multiple: false, + library: { type: 'image' } + } ); + + frame.on('select', function() { + var selection = frame.state().get('selection').first(); + if (!selection) { + return; + } + + var attachment = selection.toJSON(); + var url = attachment.url.replace(/"/g, '"'); + var alt = (attachment.alt || '').replace(/"/g, '"'); + editor.insertContent('' + alt + ''); + } ); + + frame.open(); + } + } ); // Switch to visual/text mode. jQuery(`#wp-${editorId}-wrap .switch-tmce, #wp-${editorId}-wrap .switch-html`).on('click', function() {