prefix}aitgen_api_credentials"; // Check if the table exists $table_exists = $wpdb->get_var($wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ); $api_key = ''; if ($table_exists) { $apiKeyQry = $wpdb->get_row( $wpdb->prepare("SELECT apiKey FROM {$wpdb->prefix}aitgen_api_credentials WHERE status = %d", 1), ARRAY_A ); if ($apiKeyQry) { $api_key = isset($apiKeyQry['apiKey']) ? sanitize_text_field($apiKeyQry['apiKey']) : null; } } // Use api client use OpenAI\Client; $client = OpenAI::client($api_key); function aitgen_generateProductTitle( $title = null, $keywords = null ) { global $client; // Construct the prompt based on the provided parameters $prompt = 'Generate 5 professional and SEO friendly product title'; if (!$title ) { return null; } if ($title) { $prompt .= " based on {$title}"; } // Make the API call $result = $client->chat()->create([ 'model' => 'gpt-3.5-turbo', 'messages' => [ ['role' => 'user', 'content' => $prompt], ], 'max_tokens' => 4000, 'temperature' => 0.7 ]); // Extract and return the generated product title from the API response return $result['choices'][0]['message']['content']; } function aitgen_generateShortDescription( $description = null, $keywords = null ) { global $client; // Construct the prompt based on the provided parameters $prompt = 'Generate professional and SEO friendly short description'; if (!$description ) { return null; } if ($description) { $prompt .= " based on {$description}"; } // Make the API call $result = $client->chat()->create([ 'model' => 'gpt-3.5-turbo', 'messages' => [ ['role' => 'user', 'content' => $prompt], ], 'max_tokens' => 4000, 'temperature' => 0.7 ]); // Extract and return the generated product title from the API response return $result['choices'][0]['message']['content']; } // It's initial release version. /** * Example of how you can add your own custom media * button in WordPress editor */ function aitgen_short_description_button() { global $post; printf( '' . ' %s' . '', esc_attr( $post->ID ), __( 'Generate Short Description by AI', 'textdomain' ) ); } add_action( 'media_buttons', 'aitgen_short_description_button' );