I’m trying to build a WordPress addon that takes form input and uses AI to write content
I want to make a plugin that grabs data from a contact form, sends it to an AI service, and shows the generated content to users. I’m new to WordPress development but managed to put together some code. The issue is when I hit the submit button, nothing happens and I see “text: null” in the browser console.
main-plugin.php
<?php
/*
Plugin Name: Content Creator AI
Description: Creates articles using GPT API
Version: 1.0
Author: developer
*/
define('GPT_API_TOKEN', 'your_api_token_here');
define('GPT_ENDPOINT', 'https://api.openai.com/v1/chat');
add_shortcode('content_generator_form', 'display_generator_form');
function display_generator_form() {
ob_start();
?>
<div class="content-generator-wrapper">
<?php echo do_shortcode('[contact-form-7 id="5" title="Generator Form"]'); ?>
</div>
<?php
return ob_get_clean();
}
add_action('rest_api_init', 'setup_content_endpoint');
function setup_content_endpoint() {
register_rest_route('content-ai/v1', '/create-post', array(
'methods' => 'POST',
'callback' => 'handle_content_generation',
));
}
function handle_content_generation($request) {
$data = $request->get_params();
$subject = sanitize_text_field($data['post_subject']);
$tone = sanitize_text_field($data['content_tone']);
$summary = sanitize_textarea_field($data['brief_summary']);
$user_prompt = "Subject: $subject\nTone: $tone\nSummary: $summary\nCreate article:";
$payload = array(
'prompt' => $user_prompt,
'temperature' => 0.7,
'max_tokens' => 400,
'n' => 1,
);
$request_args = array(
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . GPT_API_TOKEN,
),
'body' => json_encode($payload),
'timeout' => 25,
);
$api_response = wp_remote_post(GPT_ENDPOINT . '/completions', $request_args);
if (is_wp_error($api_response)) {
return new WP_Error('request_failed', $api_response->get_error_message());
} else {
$response_body = wp_remote_retrieve_body($api_response);
$parsed_data = json_decode($response_body, true);
$generated_content = $parsed_data['choices'][0]['text'];
return array('text' => $generated_content);
}
}
add_action('wp_enqueue_scripts', 'load_generator_scripts');
function load_generator_scripts() {
wp_enqueue_script('content-generator', plugins_url('/scripts/generator.js', __FILE__), array('jquery'));
wp_localize_script('content-generator', 'generatorConfig', array(
'endpoint' => rest_url('content-ai/v1/create-post'),
'security' => wp_create_nonce('wp_rest'),
));
}
?>
generator.js
jQuery(function($) {
$('#content-form').on('submit', function(e) {
e.preventDefault();
var formInfo = $(this).serialize();
$.ajax({
url: generatorConfig.endpoint,
method: 'POST',
data: formInfo,
beforeSend: function(xhr) {
xhr.setRequestHeader('X-WP-Nonce', generatorConfig.security);
},
success: function(data) {
if (data.text) {
$('#generated-content').html(data.text);
} else {
$('#generated-content').html('No content received');
}
},
error: function(xhr) {
$('#generated-content').html('Request failed: ' + xhr.responseText);
}
});
});
});
Anyone know why I’m getting null responses from the API call?