WordPress文章标题自动翻译为英语

  1. function translate_chinese_post_title_to_en_for_slug( $title ) {
  2. /*
  3. transtype:
  4. trans
  5. realtime
  6. */
  7. $translation_render = 'http://fanyi.baidu.com/v2transapi?from=zh&to=en&transtype=realtime&simple_means_flag=3&query='.$title;
  8. $wp_http_get = wp_safe_remote_get( $translation_render );
  9. if ( empty( $wp_http_get->errors ) ) {
  10. if ( ! empty( $wp_http_get['body'] ) ) {
  11. $trans_result = json_decode( $wp_http_get['body'], true );
  12. $trans_title = $trans_result['trans_result']['data'][0]['dst'];
  13. return $trans_title;
  14. }
  15. }
  16. return $title;
  17. }
  18. add_filter( 'sanitize_title', 'translate_chinese_post_title_to_en_for_slug', 1 );

 

image