jeg bruker jetengine og jetformbuider fra Crocoblock. jeg har satt opp et skjema i jetformbuilder for front end publisering av posts. jeg vil gjerne implementere mulighet for å bruke chat gpt i wysiwyg feltet for post innholdet i jetformbuilder, mer

WPTurbo » Snippets » jeg bruker jetengine og jetformbuider fra Crocoblock. jeg har satt opp et skjema i jetformbuilder for front end publisering av posts. jeg vil gjerne implementere mulighet for å bruke chat gpt i wysiwyg feltet for post innholdet i jetformbuilder, mer
0

Created with:

Visibility: 

public

Creator: Bent Fossan

Customize with WPTurbo AI
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.

Register an account to save your snippets or go Pro to get more features.