Created
January 29, 2024 12:57
-
-
Save esedic/8d739648ae27443a6ab281f765362f1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
I would like to get a plugin code for Wordpress which goes trough all posts, foreach trough each of them, | |
get an short meta description from main description using Microsoft OpenAI API access. | |
This plugin / code should be run trough CRON commands. Can you generate code for me? | |
------------------------------------------------------------------------------------- | |
Certainly, I can help you with a basic outline for a WordPress plugin that does what you've described. | |
Please note that you'll need to have the Microsoft OpenAI API credentials and the appropriate library installed on your server. | |
Also, you should handle error cases and edge cases based on your specific needs. | |
Here's a basic example using PHP for the plugin: | |
Create a new folder for your plugin: | |
In your WordPress plugins directory (wp-content/plugins/), create a new folder for your plugin, e.g., openai-meta-extractor. | |
Create the main plugin file: | |
In your new folder, create a file named openai-meta-extractor.php and add the following code: | |
*/ | |
<?php | |
/** | |
* Plugin Name: OpenAI Meta Extractor | |
* Description: Fetches short meta descriptions using Microsoft OpenAI API for each post and updates the meta data. | |
* Version: 1.0 | |
* Author: Your Name | |
*/ | |
// Hook into the cron event | |
register_activation_hook(__FILE__, 'openai_schedule_cron'); | |
// Add the cron schedule | |
function openai_schedule_cron() { | |
if (!wp_next_scheduled('openai_cron_event')) { | |
wp_schedule_event(time(), 'daily', 'openai_cron_event'); | |
} | |
} | |
// Define the cron function | |
add_action('openai_cron_event', 'openai_process_posts'); | |
// Function to process posts and update meta descriptions | |
function openai_process_posts() { | |
// Query all posts | |
$args = array( | |
'post_type' => 'post', // You can adjust this based on your post types | |
'posts_per_page' => -1, | |
); | |
$posts = new WP_Query($args); | |
if ($posts->have_posts()) { | |
while ($posts->have_posts()) { | |
$posts->the_post(); | |
// Get post content | |
$post_content = get_the_content(); | |
// Call the function to get short meta description using OpenAI API | |
$meta_description = openai_get_meta_description($post_content); | |
// Update post meta | |
update_post_meta(get_the_ID(), '_openai_meta_description', $meta_description); | |
} | |
wp_reset_postdata(); | |
} | |
} | |
// Function to get short meta description using OpenAI API | |
function openai_get_meta_description($content) { | |
// Replace this with your OpenAI API code | |
// Make sure to handle API request/response appropriately | |
// Example: Replace 'YOUR_OPENAI_API_KEY' with your actual API key | |
$api_key = 'YOUR_OPENAI_API_KEY'; | |
$api_url = 'https://api.openai.com/v1/engines/davinci/completions'; | |
$data = array( | |
'prompt' => $content, | |
'max_tokens' => 50, // Adjust as needed | |
); | |
$response = wp_safe_remote_post($api_url, array( | |
'headers' => array( | |
'Content-Type' => 'application/json', | |
'Authorization' => 'Bearer ' . $api_key, | |
), | |
'body' => json_encode($data), | |
)); | |
// Process API response and extract the short description | |
$meta_description = ''; // Extract the relevant data from the API response | |
return $meta_description; | |
} | |
/* | |
This is a basic outline, and you'll need to adapt it to your specific needs. | |
Make sure to replace 'YOUR_OPENAI_API_KEY' with your actual OpenAI API key. | |
Also, customize the API endpoint and parameters based on the OpenAI API documentation. | |
Activate the plugin in your WordPress admin, and the cron job will run daily, updating the meta descriptions | |
for each post. Keep in mind that handling API requests securely and efficiently is crucial, | |
and you may need to implement additional error handling and logging based on your requirements. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment