WordPress 文章自动翻译英文别名

经杀手测试,全站会卡

添加到当前主题的 functions.php 文件即可:

1:百度翻译版

  1. // 自动生成英文别名 - 百度版
  2. function bymt_slug_auto_translate( $title ) {
  3.     $wp_http_get = wp_safe_remote_get( 'http://fanyi.baidu.com/v2transapi?from=zh&to=en&transtype=trans&query=' . $title );
  4.     if ( emptyempty$wp_http_get->errors ) ) {
  5.         if ( ! emptyempty$wp_http_get['body'] ) ) {
  6.             $trans_result = json_decode( $wp_http_get['body'], true );
  7.             $trans_title = $trans_result['trans_result']['data'][0]['dst'];
  8.             return $trans_title;
  9.         }
  10.     }
  11.     return;
  12. }
  13. add_filter( 'sanitize_title', 'bymt_slug_auto_translate', 1 );

2:微软版

  1. // 自动生成英文别名 - 微软版
  2. function bymt_slug_auto_translate( $title ) {
  3.     $wp_http_get = wp_safe_remote_get( 'http://api.microsofttranslator.com/v2/ajax.svc/TranslateArray2?appId=%22TKT68kjRgkUbVtIKst6Vo0Hxnb6g2f0K3tUMyn1gZ7nc*%22&from="zh"&to="en"&options={}&texts=["' . $title . '"]' );
  4.     if ( emptyempty$wp_http_get->errors ) ) {
  5.         if ( ! emptyempty$wp_http_get['body'] ) ) {
  6.             preg_match('/"TranslatedText":"([^"]+)/i', $wp_http_get['body'], $trans_result);
  7.             $trans_title = $trans_result[1];
  8.             return $trans_title;
  9.         }
  10.     }
  11.     return;
  12. }
shashou47

发表评论取消回复

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: