LoginSignup
4
3

More than 5 years have passed since last update.

WordPressのフィードでInput is not proper UTF-8, indicate encoding !が出た時の対応

Last updated at Posted at 2015-03-18

wordpressのフィードのエラー

WordPressで、フィードにエラーが出ることがあります。

This page contains the following errors:

error on line 30 at column 25: Input is not proper UTF-8, indicate encoding !
Bytes: 0x0B 0xE3 0x82 0xAA
Below is a rendering of the page up to the first error.

わけわかんないエラーですが、0x0Bが入ってるよと言っているので、垂直タブだってことが分かります。そして丹念に指定された行を見て、コピーしてエディタにペーストすれば、おかしな制御記号が入ってることもわかります。あとは、記事を編集して不正な文字を取り除けばオッケー。

だけど、その対応って普通の人は結構難しい。0Bが垂直タブだなんて知らないし、制御記号とか見えないし。「垂直タブが入らないように気をつけて下さい」ってアナウンスも厳しい。

ということで無理やり改造

本当はプラグインとして「足す」のがお行儀がいいんだけど、面倒なのでWordPressのコードを直接書き換えちゃう。

functions.php
function do_feed() {
  global $wp_query;

  $feed = get_query_var( 'feed' );
  // Remove the pad, if present.
  $feed = preg_replace( '/^_+/', '', $feed );

  if ( $feed == '' || $feed == 'feed' )
    $feed = get_default_feed();
    $hook = 'do_feed_' . $feed;
    if ( !has_action($hook) ) {
      $message = sprintf( __( 'ERROR: %s is not a valid feed template.' ), esc_html($feed));
      wp_die( $message, '', array( 'response' => 404 ) );
    }
    ob_start("feed_callback"); //追加
    do_action( $hook, $wp_query->is_comment_feed );
    ob_end_flush(); //追加
}
//コールバックを追加
function feed_callback($buffer){
  return (str_replace("\x0B", "", $buffer));    
}

「追加」ってコメントしているところだけ修正しています。

do_feedでフィードを出力しているので、ob_startとob_end_flushではさんで出力をバッファリングする。出力する直前のコールバックで、出力バッファの中から、垂直タブを取り除く、ということをしています。垂直タブの他にもRSSとしてエラーになりそうな文字があったら、その子たちも削除する処理を加える感じで。

4
3
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
4
3