LoginSignup
0
0

chatGPTのAPIを使用してワードプレスで日本語記事を英語に翻訳するプラグイン

Last updated at Posted at 2023-06-25

chatGPTのAPIを使用してワードプレスで日本語記事を英語に翻訳するプラグイン

動作環境と手順

  • php8.1
  • openai-php/client
  • ワードプレスのプラグイン階層にchatgpt-translate-to-englishのディレクトリ作成。
  • 下記のファイルをディレクトリの中に作成。
  • 同階層(chatgpt-translate-to-english)にcomopserをインストール。
  • 同階層にて下記のコマンドを実行してください(Terminalなどを使用して)。
    composer install
  • コマンド実行後、同階層にvendorが作成されたことを確認。
  • chatgpt-translate-to-english.phpファイルを開き$APIKEY変数にchatGPTのAPIKEYを入力し同ファイルを保存してください(APIKEYの取得に関しては割愛します)。
  • ワードプレスの管理画面にログインしプラグインを有効にしてください。
    ※余談、処理が遅いので翻訳に時間がかかります。また、たまに英語を日本語に翻訳してくれる場合があり。
    ※ルールは御自身で変更してください。
chatgpt-translate-to-english.php
<?php
/*
Plugin Name: chatgpt-translate-to-english
Version: 1.0.0
Description: 記事を英語に変換する
Author: @taoka toshiaki
Author URI: https://twitter.com/toshiaki_taoka
*/
if (!defined('ABSPATH')) {
    exit;
}

/**
 * @param int $post_id
 * @param object $post
 */
function translate_to_english($post_id, $post)
{
    
    require(plugin_dir_path(__FILE__) . "vendor/autoload.php");
    $data = [];
    foreach (['post_title' => $post->post_title, "post_content" => $post->post_content] as $key => $val) {
        $data[$key] = (function ($jp = "",$key="") {
            $APIKEY = "ここにAPIKEYを入力";
            if (!$jp) return $jp;
            $responseData = $jp;

            $client = OpenAI::client($APIKEY);
            $result = $client->chat()->create([
                'model' => 'gpt-3.5-turbo',
                'messages' => [
                    ["role" => "system", "content" => '日本語文章を英語に翻訳してください。'],
                    ["role" => "system", "content" => 'html表記はそのまま文章に組み込んでください。'],
                    ['role' => 'user', 'content' => $jp],
                ]
            ]);
            if (isset($result->choices[0]->message->content)) {
                $responseData = $result->choices[0]->message->content;
                if($key === "post_content"){
                    $textdata = explode("\n",$result->choices[0]->message->content);
                    foreach($textdata as $key=>$val){
                        $textdata[$key] = "<p>".$val."</p>";
                    }
                    $responseData = "<!-- wp:paragraph -->".implode("",$textdata)."<!-- /wp:paragraph -->";
                }
            }
            return $responseData;
        })($val,$key);
    }
    $new_post = array(
        'ID'           => $post_id,
        'post_title'   => $data["post_title"],
        'post_content' => $data["post_content"],
    );
    remove_action('save_post', 'translate_to_english', 10, 2);
    wp_update_post($new_post);
    add_action('save_post', 'translate_to_english', 10, 2);
}

add_action('save_post', 'translate_to_english', 10, 2);
composer.json
{
    "require": {
        "openai-php/client": "^0.6.2",
        "symfony/http-client": "^6.3",
        "nyholm/psr7": "^1.8"
    },
    "config": {
        "allow-plugins": {
            "php-http/discovery": true
        }
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0