Wordpress plugin and chat gpt api problem

i made wordpress plugin below.
issue: after form submitting, returns null

wordpress plugin:

// Register the shortcode
add_shortcode(‘chatgpt’, ‘chatgpt_shortcode’);

// Define the shortcode function
function chatgpt_shortcode($atts) {
// Extract shortcode attributes
$atts = shortcode_atts(array(
‘api_key’ => ‘Fills by my API key’,
), $atts);
$api_key = $atts[‘api_key’];

// Get user input and send request to ChatGPT
if (isset($_POST[‘user_input’])) {
$user_input = $_POST[‘user_input’];
$response = gpt_request($api_key, $user_input);
return $response;

// Display user input form

<?php return ob_get_clean(); }

// Define the ChatGPT request function
function gpt_request($api_key, $user_input) {
$url = ‘https://api.openai.com/v1/engines/davinci-codex/completions’;
$prompt = 'Please help me with ’ . $user_input . ‘.’;
$headers = array(
‘Content-Type: application/json’,
'Authorization: Bearer ’ . $api_key,
$data = array(
‘prompt’ => $prompt,
‘max_tokens’ => 100,
‘temperature’ => 0.7,
$options = array(
‘http’ => array(
‘header’ => $headers,
‘method’ => ‘POST’,
‘content’ => json_encode($data),
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$response = json_decode($response, true);
return $response[‘choices’][0][‘text’];

> 'Authorization: Bearer ’ . $api_key,

correct?  Should the colon be adjusted to proceed the word `Bearer`?

Upon further review I see that this is the proper syntax. Correcting to prevent confusion.

the code is generated by CHAT GPT. so it’s correct I think

In this case I would suggest having chatGPT explain the components of the code chronologically so that they have a better understanding of the underlying functionality of what the module is attempting to accomplish.

Why would you think that?

ChatGPT tends to “decent first drafts” but generally speaking the code is “not correct” unless only a few lines for a simple, short method; and even then every line must be validated by human coder!

you’re right chatGPT but, what about solution?!

I’d suggest a viable solution in which you use chatGPT to break up the module into smaller elements that you can implement and validate functionality in a more step-by-step manner, rather than attempting to have a finished product in one go. Build it from the ground up, starting with a simplified version. Also, you could try creating a framework for the module without OpenAI functionality integrated, and add it later when the template is set and functioning.