# Writing a Wordpress Plugin with Chat GPT From Scratch ### Prompt 1: write a php script that works in wordpress that uses the OpenAI Api to execute a prompt where the subject of the prompt is defined by an input form ### Output... Here is an example script that creates an input form in WordPress, uses the OpenAI API to execute a prompt based on the subject defined by the input form, and displays the response: ``` <?php // Check if the form has been submitted if(isset($_POST['submit'])) { // Increase execution timeout set_time_limit(60); // Set up API key $api_key = 'YOUR_API_KEY'; // Define the subject from the form input $subject = $_POST['subject']; // Define the base prompt $prompt = "what is {$subject}?"; // Execute the prompt $response = wp_remote_post( 'https://api.openai.com/v1/engines/text-davinci-002/completions', array( 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key ), 'body' => json_encode( array( 'prompt' => $prompt, 'max_tokens' => 2048 ) ) ) ); // Check for error if ( is_wp_error( $response ) ) { echo 'Error: ' . $response->get_error_message(); exit; } // Decode the response $response = json_decode( $response['body'], true ); // Print the response echo $response['choices'][0]['text']; } else { // Display the form ?> <form method="post"> <label for="subject">Enter the subject:</label> <input type="text" id="subject" name="subject"> <input type="submit" value="Submit" name="submit"> </form> <?php } ?> ``` ### Prompt 2: can you write a wordpress plugin that uses the above code? **Output:** Here is an example of a basic WordPress plugin that uses the above code to execute a prompt based on a subject entered by the user: ``` <?php /* Plugin Name: OpenAI Prompt Generator Plugin URI: https://example.com Description: A plugin that generates a prompt based on a subject entered by the user using the OpenAI API Version: 1.0 Author: John Doe Author URI: https://example.com */ function openai_prompt_generator_form() { // Check if the form has been submitted if(isset($_POST['submit'])) { // Increase execution timeout set_time_limit(60); // Set up API key $api_key = 'YOUR_API_KEY'; // Define the subject from the form input $subject = $_POST['subject']; // Define the base prompt $prompt = "what is {$subject}?"; // NOTE AT THIS POINT I HAD TO PROMPT GPT TO CONTINUE WRITING PLUGIN // prompt 3: continue writing prompt // Execute the prompt $response = wp_remote_post( 'https://api.openai.com/v1/engines/text-davinci-002/completions', array( 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $api_key ), 'body' => json_encode( array( 'prompt' => $prompt, 'max_tokens' => 2048 ) ) ) ); // Check for error if ( is_wp_error( $response ) ) { echo 'Error: ' . $response->get_error_message(); exit; } // Decode the response $response = json_decode( $response['body'], true ); // Print the response echo $response['choices'][0]['text']; } else { // Display the form ?> // NOTE AT THIS POINT I HAD TO PROMPT GPT TO CONTINUE WRITING PLUGIN // prompt 4: continue writing prompt <form method="post"> <label for="subject">Enter the subject:</label> <input type="text" id="subject" name="subject"> <input type="submit" value="Submit" name="submit"> </form> <?php } } // Add a shortcode to display the form add_shortcode('openai_prompt_generator', 'openai_prompt_generator_form'); // Activation and Deactivation hooks register_activation_hook( _FILE_, 'openai_prompt_generator_activate' ); register_deactivation_hook( _FILE_, 'openai_prompt_generator_deactivate' ); function openai_prompt_generator_activate() { // Do plugin activation tasks here } function openai_prompt_generator_deactivate() { // Do plugin deactivation tasks here } ``` ### It continued... This plugin will add a shortcode `[openai_prompt_generator]` that can be used to display the form on any page or post.<br/>Activation and deactivation hooks have been added as well to perform any necessary actions when the plugin is activated or deactivated. Please note that as before, you need to replace "YOUR_API_KEY" with your API key which you can get from the OpenAI website.<br/>You can also change the engine to "text-curie-001" or any other you have access to. In order to use this plugin on a WordPress website you need to have the correct permissions to upload, install and activate a plugin.<br/>Also, if you're using this plugin on a website that is not hosted on your own server, make sure that the host allows outbound connections to api.openai.com It is also worth mentioning that increasing the execution time limit too much may cause performance issues, so it's always a good practice to optimize your code as much as possible and avoid long-running scripts.