Try this:
To retrieve the content of the wp_editor field when the form is submitted and send it via AJAX, you need to make a few adjustments to your JavaScript code. The issue you’re encountering with tinyMCE.get(“email_body”).getContent() is likely due to the timing of when this code is executed or the editor instance not being correctly referenced.
Here’s a modified version of your JavaScript code that should correctly retrieve the content from the wp_editor and send it through your AJAX call:
jQuery(document).ready(function($) {
$(".email-submit").on("click", function(event) {
event.preventDefault();
var inquiryId = $(this).data("inquiry-id");
var nonce = $(this).data("nonce");
// Retrieve the content of the wp_editor
var emailBody;
if (typeof tinyMCE !== 'undefined' && tinyMCE.get('email_body') && !tinyMCE.get('email_body').isHidden()) {
emailBody = tinyMCE.get('email_body').getContent();
} else {
emailBody = $('#email_body').val();
}
// Open the email composer modal
openEmailComposer(inquiryId, nonce, emailBody);
});
function openEmailComposer(inquiryId, nonce, emailBody) {
// AJAX call to the PHP function handling email composition
$.ajax({
url: ajaxurl,
type: "POST",
data: {
action: "compose_email",
nonce: nonce,
inquiry_id: inquiryId,
email_body: emailBody,
},
success: function(response) {
if (response.success) {
console.log(response);
} else {
alert("Error opening email composer.");
}
},
error: function() {
alert("Error opening email composer.");
},
});
}
});
In this modified code, the emailBody variable is set using a conditional statement that checks if the TinyMCE editor is defined and not hidden. If TinyMCE is active, it retrieves the content from the editor; otherwise, it falls back to the standard textarea value. This approach ensures compatibility with different scenarios, such as when TinyMCE is not loaded or when the user is working in the “Text” tab of the editor instead of the “Visual” tab.