├── README.md ├── chatgpt-wordpress-api.zip ├── chatgpt.php └── simple-form-ajax.js /README.md: -------------------------------------------------------------------------------- 1 | # -chatgpt-wordpress-api 2 | Wordpress plugin to add chatgpt into your Wordpress site 3 | 4 | How to use the plugin 5 | 6 | Step1: Install the plugin (Zip file) 7 | Step2: Add your secret key (SK) into the backend form and save. 8 | Step3: There will be shortcode in the form display. Add that code to the frontend where you want to show the frontend form 9 | -------------------------------------------------------------------------------- /chatgpt-wordpress-api.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anmol65233/-chatgpt-wordpress-api/4f6824e30a3326915c168a78323bc6bc09324029/chatgpt-wordpress-api.zip -------------------------------------------------------------------------------- /chatgpt.php: -------------------------------------------------------------------------------- 1 | prefix . 'chat_key'; 12 | $charset_collate = $wpdb->get_charset_collate(); 13 | 14 | $sql = "CREATE TABLE $table_name ( 15 | id int(9) NOT NULL AUTO_INCREMENT, 16 | publickey varchar(100) NOT NULL, 17 | PRIMARY KEY (id) 18 | ) $charset_collate;"; 19 | 20 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 21 | dbDelta($sql); 22 | } 23 | register_activation_hook(__FILE__, 'my_plugin_activate'); 24 | 25 | 26 | // Create a form 27 | function my_plugin_form() 28 | { 29 | global $wpdb; 30 | $table_name = $wpdb->prefix . "chat_key"; 31 | if (isset($_POST['publickey'])) { 32 | $key = $_POST['publickey']; 33 | $checkIfExists = $wpdb->get_var("SELECT id FROM $table_name"); 34 | if ($checkIfExists > 0) { 35 | $wpdb->update( 36 | $table_name, 37 | array( 38 | 'publickey' => $key 39 | ), 40 | array('id' => $checkIfExists) 41 | ); 42 | } else { 43 | $wpdb->insert( 44 | $table_name, 45 | array( 46 | 'publickey' => $key 47 | ) 48 | ); 49 | } 50 | } 51 | 52 | $getPuclickey = $wpdb->get_var("SELECT publickey FROM $table_name"); 53 | 54 | echo '
'; 55 | echo ''; 56 | echo '

'; 57 | echo ''; 58 | echo '
'; 59 | echo 'Add shortcode simple_form_ajax_chatgpt'; 60 | } 61 | 62 | // Show the form 63 | function my_plugin_display_form() 64 | { 65 | add_menu_page('ChatGPT Integration', 'ChatGPT Integration', 'manage_options', 'my-form', 'my_plugin_form', 'dashicons-admin-generic', 6); 66 | } 67 | add_action('admin_menu', 'my_plugin_display_form'); 68 | 69 | 70 | 71 | // Enqueue the script for the AJAX form submission 72 | function simple_form_ajax_enqueue_scripts() 73 | { 74 | wp_enqueue_script('simple-form-ajax', plugin_dir_url(__FILE__) . 'simple-form-ajax.js', array('jquery'), '1.0', true); 75 | wp_localize_script('simple-form-ajax', 'simple_form_ajax_params', array( 76 | 'ajax_url' => admin_url('admin-ajax.php') 77 | )); 78 | } 79 | add_action('wp_enqueue_scripts', 'simple_form_ajax_enqueue_scripts'); 80 | 81 | //create shortcode to display the form 82 | function simple_form_ajax_form_shortcode() 83 | { 84 | ob_start(); 85 | ?> 86 |
87 | 88 | 89 |
90 |
91 | prefix . "chat_key"; 102 | $question = $_POST['form_data']; 103 | if (!empty($question)) { 104 | $sk = $wpdb->get_var("SELECT publickey FROM $table_name"); 105 | $ch = curl_init(); 106 | curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions'); 107 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 108 | curl_setopt($ch, CURLOPT_POST, 1); 109 | curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"model\": \"text-davinci-003\", \"prompt\": \"$question\", \"temperature\": 0, \"max_tokens\": 100}"); 110 | $headers = array(); 111 | $headers[] = 'Content-Type: application/json'; 112 | $headers[] = "Authorization: Bearer $sk"; 113 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 114 | $result = curl_exec($ch); 115 | if (curl_errno($ch)) { 116 | echo 'Error:' . curl_error($ch); 117 | } 118 | curl_close($ch); 119 | $result = json_decode($result, 1); 120 | $answer = $result['choices'][0]['text']; 121 | echo json_encode(array('succress' => true)); 122 | } 123 | wp_die(); 124 | } 125 | add_action('wp_ajax_simple_form_ajax_submit', 'simple_form_ajax_submit'); 126 | add_action('wp_ajax_nopriv_simple_form_ajax_submit', 'simple_form_ajax_submit'); 127 | ?> -------------------------------------------------------------------------------- /simple-form-ajax.js: -------------------------------------------------------------------------------- 1 | jQuery(document).ready(function($) { 2 | $('#simple-form-ajax').on('submit', function(e) { 3 | e.preventDefault(); 4 | //var formData = $(this).serialize(); 5 | var formData = $('#question').val(); 6 | $.ajax({ 7 | type: 'POST', 8 | url: simple_form_ajax_params.ajax_url, 9 | data: { 10 | action: 'simple_form_ajax_submit', 11 | form_data: formData 12 | }, 13 | success: function(response) { 14 | console.log(response); 15 | // var returnedData = JSON.parse(response); 16 | // $('#output').html(returnedData.succress); 17 | // Do something with the response 18 | } 19 | }); 20 | }); 21 | }); 22 | --------------------------------------------------------------------------------