程式碼:
<?php
/*
Plugin Name:zimeng_slug_translator
Plugin URI: https://chenxuehu.com/
Description: 呼叫有道翻譯API,自動將中文標題翻譯成英語.
Version: 1.0.0
Author: Xuehu
Author URI:https://chenxuehu.com/
License: GPL
*/
define("CURL_TIMEOUT", 20);
define("URL", "http://openapi.youdao.com/api");
define("APP_KEY", "");
define("SEC_KEY", "");
/**
* 有道API翻譯入口
*/
if( !function_exists("zimeng_youdao_translate")){
function zimeng_youdao_translate($query, $from, $to)
{
$args = array(
'q' => $query,
'appKey' => APP_KEY,
'salt' => rand(10000,99999),
'from' => $from,
'to' => $to,
);
$args['sign'] = md5(APP_KEY.$query.$args['salt'].SEC_KEY);
$ret = zimeng_youdao_call(URL, $args);
$ret = json_decode($ret, true);
return $ret['translation'][0];
}
}
if( !function_exists("zimeng_youdao_call")){
function zimeng_youdao_call($url, $args=null, $method="post", $testflag = 0, $timeout = CURL_TIMEOUT, $headers=array())
{
$ret = false;
$i = 0;
while($ret === false)
{
if($i > 1)
break;
if($i > 0)
{
sleep(1);
}
$ret = zimeng_youdao_callOnce($url, $args, $method, false, $timeout, $headers);
$i++;
}
return $ret;
}
}
if( !function_exists("zimeng_youdao_callOnce")){
function zimeng_youdao_callOnce($url, $args=null, $method="post", $withCookie = false, $timeout = CURL_TIMEOUT, $headers=array())
{
$ch = curl_init();
if($method == "post")
{
$data = zimeng_youdao_convert($args);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
}
else
{
$data = zimeng_youdao_convert($args);
if($data)
{
if(stripos($url, "?") > 0)
{
$url .= "&$data";
}
else
{
$url .= "?$data";
}
}
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(!empty($headers))
{
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
if($withCookie)
{
curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
}
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
}
if( !function_exists("zimeng_youdao_convert")){
function zimeng_youdao_convert(&$args)
{
$data = '';
if (is_array($args))
{
foreach ($args as $key=>$val)
{
if (is_array($val))
{
foreach ($val as $k=>$v)
{
$data .= $key.'['.$k.']='.rawurlencode($v).'&';
}
}
else
{
$data .="$key=".rawurlencode($val)."&";
}
}
return trim($data, "&");
}
return $args;
}
}
if( !function_exists("ZimengSlugTrans")){
function ZimengSlugTrans($postID){
global $wpdb;
$tableposts = $wpdb->posts ;
$sql = "SELECT post_title,post_name FROM $tableposts WHERE ID=$postID";
$res = $wpdb->get_results($sql);
$post_title = $res[0]->post_title;
$tran_title = zimeng_youdao_translate($post_title,"zh-CHS","EN");
$slug = $tran_title;
if(function_exists("sanitize_title") ) {
if( sanitize_title( $res[0]->post_title ) != $res[0]->post_name ){
if( !substr_count($path, '%') )
return true;
}
$slug = sanitize_title( $slug);
if( strlen($slug) < 2 ) return true;
}
$sql ="UPDATE ".$tableposts." SET `post_name` = '".$slug."' WHERE ID =$postID;";
$res = $wpdb->query($sql);
}
}
add_action('publish_post', 'ZimengSlugTrans');
add_action('edit_post', 'ZimengSlugTrans');
複製程式碼
做成了外掛,但是沒有做後臺介面,要使用需要手動的修改檔案,設定以下的資訊為您的申請的資訊:
define("APP_KEY", "");
define("SEC_KEY", "");
複製程式碼
該翻譯的API是付費的,但可以試用,註冊即送100元體驗金,應該可以用一段時間。
外掛版本下載,修改資訊後,後臺啟用外掛即可。