<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adil El Hallaoui</title>
    <description>The latest articles on DEV Community by Adil El Hallaoui (@adil-hl).</description>
    <link>https://dev.to/adil-hl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3485037%2Fe060a526-fac8-45e3-8e42-6911fe18aaaa.webp</url>
      <title>DEV Community: Adil El Hallaoui</title>
      <link>https://dev.to/adil-hl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adil-hl"/>
    <language>en</language>
    <item>
      <title>Auto-Translate WordPress Posts with GPT + WPML (Build Your Own Plugin)</title>
      <dc:creator>Adil El Hallaoui</dc:creator>
      <pubDate>Sun, 07 Sep 2025 23:04:58 +0000</pubDate>
      <link>https://dev.to/adil-hl/auto-translate-wordpress-posts-with-gpt-wpml-build-your-own-plugin-138b</link>
      <guid>https://dev.to/adil-hl/auto-translate-wordpress-posts-with-gpt-wpml-build-your-own-plugin-138b</guid>
      <description>&lt;p&gt;Managing multilingual sites is hard. Writing the same post three times? Even harder. What if WordPress could auto-translate your posts into multiple languages the moment you hit “Publish”?&lt;br&gt;
In this tutorial, we’ll build a WordPress plugin that connects WPML with GPT, automatically generating translated versions of your content.&lt;/p&gt;

&lt;p&gt;✨ What You’ll Learn&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to create a custom WordPress plugin&lt;/li&gt;
&lt;li&gt;How to hook into the save_post action&lt;/li&gt;
&lt;li&gt;How to use WPML APIs to manage translations&lt;/li&gt;
&lt;li&gt;How to integrate GPT for real-time translation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;🔧 Step 1: Set Up the Plugin Skeleton&lt;br&gt;
Inside wp-content/plugins/, create a folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;auto-gpt-translator/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside it, add a file: auto-gpt-translator.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
/**
 * Plugin Name: Auto GPT Translator for WPML
 * Description: Automatically translates WordPress posts into multiple languages using GPT and WPML.
 * Version: 1.0
 * Author: Your Name
 */

if ( ! defined( 'ABSPATH' ) ) exit;

class AutoGPTTranslator {

    public function __construct() {
        add_action('save_post', [$this, 'auto_translate_post'], 20, 2);
    }

    public function auto_translate_post($post_id, $post) {
        if ( wp_is_post_revision($post_id) ) return;

        $languages = apply_filters('wpml_active_languages', NULL, ['skip_missing' =&amp;gt; 0]);

        if (!$languages) return;

        foreach ($languages as $lang =&amp;gt; $details) {
            if ($lang === apply_filters('wpml_default_language', NULL)) continue;

            $this-&amp;gt;translate_and_save($post_id, $post, $lang);
        }
    }

    private function translate_and_save($post_id, $post, $lang) {
        $translated_content = $this-&amp;gt;call_gpt_translation($post-&amp;gt;post_content, $lang);
        $translated_title   = $this-&amp;gt;call_gpt_translation($post-&amp;gt;post_title, $lang);

        do_action('wpml_update_translated_post', [
            'element_id'    =&amp;gt; $post_id,
            'trid'          =&amp;gt; apply_filters('wpml_element_trid', NULL, $post_id, 'post_post'),
            'language_code' =&amp;gt; $lang,
            'source_language_code' =&amp;gt; apply_filters('wpml_default_language', NULL),
            'post_content'  =&amp;gt; $translated_content,
            'post_title'    =&amp;gt; $translated_title,
        ]);
    }

    private function call_gpt_translation($text, $target_lang) {
        $api_key = get_option('auto_gpt_api_key');
        if (!$api_key) return $text;

        $response = wp_remote_post("https://api.openai.com/v1/chat/completions", [
            'headers' =&amp;gt; [
                'Content-Type' =&amp;gt; 'application/json',
                'Authorization' =&amp;gt; "Bearer $api_key",
            ],
            'body' =&amp;gt; json_encode([
                'model' =&amp;gt; 'gpt-4o-mini',
                'messages' =&amp;gt; [
                    ['role' =&amp;gt; 'system', 'content' =&amp;gt; "Translate the following text into $target_lang."],
                    ['role' =&amp;gt; 'user', 'content' =&amp;gt; $text]
                ],
            ]),
        ]);

        if ( is_wp_error($response) ) return $text;

        $body = json_decode(wp_remote_retrieve_body($response), true);

        return $body['choices'][0]['message']['content'] ?? $text;
    }
}

new AutoGPTTranslator();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🔧 Step 2: Add a Settings Page&lt;br&gt;
We need a simple admin page where you can paste your OpenAI API key.&lt;br&gt;
Create settings.php inside the plugin folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
add_action('admin_menu', function() {
    add_options_page(
        'Auto GPT Translator',
        'Auto GPT Translator',
        'manage_options',
        'auto-gpt-translator',
        'auto_gpt_translator_settings_page'
    );
});

add_action('admin_init', function() {
    register_setting('auto_gpt_translator_settings', 'auto_gpt_api_key');
});

function auto_gpt_translator_settings_page() {
?&amp;gt;
&amp;lt;div class="wrap"&amp;gt;
    &amp;lt;h1&amp;gt;Auto GPT Translator Settings&amp;lt;/h1&amp;gt;
    &amp;lt;form method="post" action="options.php"&amp;gt;
        &amp;lt;?php settings_fields('auto_gpt_translator_settings'); ?&amp;gt;
        &amp;lt;?php do_settings_sections('auto_gpt_translator_settings'); ?&amp;gt;
        &amp;lt;table class="form-table"&amp;gt;
            &amp;lt;tr valign="top"&amp;gt;
                &amp;lt;th scope="row"&amp;gt;OpenAI API Key&amp;lt;/th&amp;gt;
                &amp;lt;td&amp;gt;&amp;lt;input type="text" name="auto_gpt_api_key" value="&amp;lt;?php echo esc_attr(get_option('auto_gpt_api_key')); ?&amp;gt;" style="width: 400px;"/&amp;gt;&amp;lt;/td&amp;gt;
            &amp;lt;/tr&amp;gt;
        &amp;lt;/table&amp;gt;
        &amp;lt;?php submit_button(); ?&amp;gt;
    &amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;?php
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚙️ Step 3: How It Works&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You publish a post in your default WPML language.&lt;/li&gt;
&lt;li&gt;The plugin detects all other active languages.&lt;/li&gt;
&lt;li&gt;For each language, it sends the post title and content to GPT.&lt;/li&gt;
&lt;li&gt;WPML stores the new translated versions automatically.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;🔒 Security Best Practices&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always use esc_attr() and sanitize_text_field() when saving settings.&lt;/li&gt;
&lt;li&gt;Validate API responses with is_wp_error().&lt;/li&gt;
&lt;li&gt;Consider adding rate limiting to avoid API overuse.&lt;/li&gt;
&lt;li&gt;Never hardcode your API key in the plugin.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🚀 What You Just Built&lt;/p&gt;

&lt;p&gt;You now have a production-ready WordPress plugin that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Auto-translates posts with GPT&lt;/li&gt;
&lt;li&gt;Integrates seamlessly with WPML&lt;/li&gt;
&lt;li&gt;Lets you manage translations without manual copy-paste&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
