admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('aidai_generate_nonce'), )); } // ==================== // 2. Activation // ==================== register_activation_hook(__FILE__, 'aidai_activate'); function aidai_activate() { $defaults = array( 'api_key' => '', ); if (!get_option('aidai_settings')) { update_option('aidai_settings', $defaults); } } // ==================== // 3. Deactivation // ==================== register_deactivation_hook(__FILE__, 'aidai_deactivate'); function aidai_deactivate() { // Keep data on deactivation } // ==================== // 4. Uninstall // ==================== register_uninstall_hook(__FILE__, 'aidai_uninstall'); function aidai_uninstall() { delete_option('aidai_settings'); } // ==================== // 4b. Settings registration (with sanitization) // ==================== add_action('admin_init', 'aidai_register_settings'); function aidai_register_settings() { register_setting('aidai_settings_group', 'aidai_settings', array( 'type' => 'array', 'sanitize_callback' => 'aidai_sanitize_settings', 'default' => array('api_key' => ''), )); } function aidai_sanitize_settings($input) { $clean = array(); $clean['api_key'] = isset($input['api_key']) ? sanitize_text_field(wp_unslash($input['api_key'])) : ''; return $clean; } // ==================== // 5. Admin menu // ==================== add_action('admin_menu', 'aidai_add_menu'); function aidai_add_menu() { add_menu_page( 'Aidai Writer', 'Aidai Writer', 'edit_posts', 'aidai-writer', 'aidai_settings_page', 'dashicons-admin-site', 55 ); } // ==================== // 6. Settings page // ==================== function aidai_settings_page() { $settings = get_option('aidai_settings'); ?>

Aidai Writer - Settings

API Key (Optional)

Enter an OpenAI API key to enable real AI generation. Without it, demo mode is used.


About Aidai Writer

Version
Author Aidan-WP
License GPL v2 or later

Enter keywords and let AI generate content for you

Title
Content
Summary
$content)); } // ==================== // 10. Core generation logic // ==================== function aidai_generate($keyword, $type, $settings) { if (!empty($settings['api_key'])) { return aidai_call_api($keyword, $type, $settings['api_key']); } return aidai_demo_generate($keyword, $type); } // ==================== // 11. OpenAI API call // ==================== function aidai_call_api($keyword, $type, $api_key) { $prompts = array( 'title' => "Generate 3 catchy article titles for the following topic, separated by newlines: $keyword", 'content' => "Write a complete article of about 800 words around the topic \"$keyword\", including introduction, body, and conclusion.", 'summary' => "Summarize the key points of the following topic in one sentence: $keyword", ); $prompt = isset($prompts[$type]) ? $prompts[$type] : $prompts['content']; $response = wp_remote_post('https://api.openai.com/v1/chat/completions', array( 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', ), 'body' => wp_json_encode(array( 'model' => 'gpt-3.5-turbo', 'messages' => array( array('role' => 'user', 'content' => $prompt), ), 'max_tokens' => 1000, 'temperature' => 0.7, )), 'timeout' => 30, )); if (is_wp_error($response)) { return 'API request failed: ' . $response->get_error_message(); } $body = json_decode(wp_remote_retrieve_body($response), true); if (isset($body['choices'][0]['message']['content'])) { return trim($body['choices'][0]['message']['content']); } return 'AI generation failed. Please check your API key.'; } // ==================== // 12. Demo mode // ==================== function aidai_demo_generate($keyword, $type) { $date = gmdate('Y-m-d'); switch ($type) { case 'title': return "Complete Guide to \"$keyword\" - [$date]"; case 'content': return "An In-depth Introduction to \"$keyword\" Paragraph 1: $keyword is a trending topic today, and more and more people are paying attention to it. Whether you are a beginner or an experienced professional, this article will help you gain a better understanding. Paragraph 2: First, we need to understand the basic concepts of $keyword. Simply put, it involves the following key points: - Core principles - Practical applications - Frequently asked questions Paragraph 3: Next, let's explore advanced techniques for $keyword. Mastering these methods can help you achieve more with less effort. Paragraph 4: In summary, although $keyword may seem complex, anyone can learn it by following the right approach step by step. The most important thing is consistent practice and hands-on experience. (Note: This is demo-generated content. Enter an API key in settings to get real AI-generated content.)"; case 'summary': return "This article covers the core knowledge and practical tips about \"$keyword\" to help readers get started quickly and deepen their understanding."; default: return "Unknown type"; } }