Skip to content

Commit 54365a5

Browse files
authored
gw-rich-text-html-fields.php: Added image upload capabilities.
1 parent 9461266 commit 54365a5

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

gravity-forms/gw-rich-text-html-fields.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
add_action( 'admin_init', function() {
1717
if ( GFForms::get_page() === 'form_editor' ) {
1818
wp_enqueue_editor();
19+
wp_enqueue_media(); // Required for media uploads
1920
}
2021
} );
2122

@@ -70,7 +71,35 @@
7071
jQuery( document).on( 'tinymce-editor-setup', function ( event, editor ) {
7172
var editorId = 'field_rich_content';
7273
if ( editor.id === editorId ) {
73-
editor.settings.toolbar1 = 'bold,italic,underline,bullist,numlist,alignleft,aligncenter,alignright,link';
74+
editor.settings.toolbar1 = 'bold,italic,underline,bullist,numlist,alignleft,aligncenter,alignright,link,image';
75+
76+
// Handle image insertion from media library
77+
editor.addButton( 'image', {
78+
icon: 'image',
79+
tooltip: 'Insert Image',
80+
onclick: function() {
81+
var frame = wp.media({
82+
title: 'Insert Media',
83+
button: { text: 'Insert into HTML Field' },
84+
multiple: false,
85+
library: { type: 'image' }
86+
} );
87+
88+
frame.on('select', function() {
89+
var selection = frame.state().get('selection').first();
90+
if (!selection) {
91+
return;
92+
}
93+
94+
var attachment = selection.toJSON();
95+
var url = attachment.url.replace(/"/g, '"');
96+
var alt = (attachment.alt || '').replace(/"/g, '"');
97+
editor.insertContent('<img src="' + url + '" alt="' + alt + '" />');
98+
} );
99+
100+
frame.open();
101+
}
102+
} );
74103

75104
// Switch to visual/text mode.
76105
jQuery(`#wp-${editorId}-wrap .switch-tmce, #wp-${editorId}-wrap .switch-html`).on('click', function() {

0 commit comments

Comments
 (0)