Skip to content

gw-rich-text-html-fields.php: Added image upload capabilities. #1075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion gravity-forms/gw-rich-text-html-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
} );

Expand Down Expand Up @@ -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('<img src="' + url + '" alt="' + alt + '" />');
} );

frame.open();
}
} );

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