0
X
Add Snippet To Project
New Project
Add To Existing Project
<?php
jQuery( document ).ready( function( $ ) {
// Show the popup when the button is clicked
$( "#chatgpt-button" ).on( "click", function() {
$( "#chatgpt-popup" ).toggle();
} );
// Handle the submission of the prompt
$( "#chatgpt-submit" ).on( "click", function() {
var prompt = $( "#chatgpt-prompt" ).val();
if ( prompt ) {
$.ajax( {
url: ajaxurl,
type: "POST",
data: {
action: "chatgpt_request",
prompt: prompt
},
success: function( response ) {
$( "#chatgpt-response" ).text( response );
},
error: function() {
$( "#chatgpt-response" ).text( "Error while fetching response." );
}
} );
} else {
$( "#chatgpt-response" ).text( "Please enter a prompt." );
}
} );
// Insert the response into the WYSIWYG field
$( "#insert-content" ).on( "click", function() {
var content = $( "#chatgpt-response" ).text();
if ( content ) {
// Assuming the WYSIWYG field has a unique selector, replace '#wysiwyg-field' with the actual selector
var wysiwygField = tinyMCE.get( 'wysiwyg-field' );
if ( wysiwygField ) {
wysiwygField.setContent( content );
}
$( "#chatgpt-popup" ).hide(); // Close the popup after inserting content
} else {
alert( "No content to insert." );
}
} );
} );
This JavaScript file will enable the functionality you described, allowing users to interact with ChatGPT and insert generated content into the WYSIWYG field.
