アイキャッチ画像がFeedlyに表示されない問題

RSSリーダーにFeedlyを使っているのですが、このサイトを登録しておこうと思って登録すると非常に残念なことに……(下図参照)

Feedly Feed一覧

アイキャッチ画像は無視されて、投稿の本文にある画像を表示している模様。まぁ、RSSフィード内にアイキャッチ画像のパスは存在しないのでそりゃあ表示できないよなぁと思いまして。じゃあ投稿の中にアイキャッチ画像を入れるというところに行き着くのですが、RSSフィードのコンテンツ出力部分にフックポイントがないかと調査を開始。

RSSフィードの投稿本文の出力箇所確認

WordPressのサイトでは、設置したドメイン(もしくはURL)の後ろに「/feed/」とつけるとRSSフィードを確認できます。フィードの形式はRSS/RSS2/Atom/RDFと出力できるようになっています。
デフォルトではRSS2が出力されるロジックになっていました。

function get_default_feed() {
	/**
	 * Filters the default feed type.
	 *
	 * @since 2.5.0
	 *
	 * @param string $feed_type Type of default feed. Possible values include 'rss2', 'atom'.
	 *                          Default 'rss2'.
	 */
	$default_feed = apply_filters( 'default_feed', 'rss2' );
	return 'rss' == $default_feed ? 'rss2' : $default_feed;
}

default_feedのフィルターフックが準備してあるので、デフォルトは変更できるみたいだな。(変えることあるかな…)
ということで、wp-includes/feed-rss2.phpの該当箇所を確認。

<?php if (get_option('rss_use_excerpt')) : ?>
		<description><![CDATA[<?php the_excerpt_rss(); ?>]]></description>
<?php else : ?>
		<description><![CDATA[<?php the_excerpt_rss(); ?>]]></description>
	<?php $content = get_the_content_feed('rss2'); ?>
	<?php if ( strlen( $content ) > 0 ) : ?>
		<content:encoded><![CDATA[<?php echo $content; ?>]]></content:encoded>
	<?php else : ?>
		<content:encoded><![CDATA[<?php the_excerpt_rss(); ?>]]></content:encoded>
	<?php endif; ?>
<?php endif; ?>

get_option('rss_use_excerpt'))は管理画面の「設定」→「表示設定」にある「RSS/Atom フィードでの各投稿の表示」による分岐。抜粋のみを表示の場合は<content:encoded>が出力されないということ。(それにしても可読性悪いな…)

RSSフィード設定画面

コード内のthe_excerpt_rss()関数で抜粋、get_the_content_feed()関数で投稿の本文を表示・取得している模様。それぞれフックポイントを探る。

the_excerpt_rss()

関数の中身を見てみると、get_the_excerpt()関数で抜粋の値を取得してechoしているだけみたい。the_excerpt_rssというフィルターが準備されているので、$outputにアイキャッチ画像をつけてあげれば良さそう。

function the_excerpt_rss() {
	$output = get_the_excerpt();
	/**
	 * Filters the post excerpt for a feed.
	 *
	 * @since 1.2.0
	 *
	 * @param string $output The current post excerpt.
	 */
	echo apply_filters( 'the_excerpt_rss', $output );
}

get_the_content_feed()

こちらの関数も中身を見てみると、the_excerpt_rss()関数と似たような感じでget_the_content()関数で投稿の本文の値を取得して戻している。こちらもthe_content_feedというフィルターが準備されているので、$contentにアイキャッチ画像をつけてあげればこちらも良さそう。

function get_the_content_feed($feed_type = null) {
	if ( !$feed_type )
		$feed_type = get_default_feed();

	/** This filter is documented in wp-includes/post-template.php */
	$content = apply_filters( 'the_content', get_the_content() );
	$content = str_replace(']]>', ']]>', $content);
	/**
	 * Filters the post content for use in feeds.
	 *
	 * @since 2.9.0
	 *
	 * @param string $content   The current post content.
	 * @param string $feed_type Type of feed. Possible values include 'rss2', 'atom'.
	 *                          Default 'rss2'.
	 */
	return apply_filters( 'the_content_feed', $content, $feed_type );
}

functions.phpに実装コードを書いてみる

要は、the_excerpt_rss()関数もget_the_content()関数で抜粋・本文を返すフィルターが準備されているので、アイキャッチ画像をその前につけてあげれば良い。それならば関数は1つで良さそうなので以下のようなコードを書きました。

/**
 * RSS Feed Thumbnail add.
 *
 * @param  string $content
 * @return string $content
 */
function rss_feed_thumbnail( $content ) {
	global $post;

	if ( has_post_thumbnail( $post->ID ) ) {
		$content = '
' . get_the_post_thumbnail($post->ID) . '
' . $content; } return $content; } add_filter( 'the_excerpt_rss', 'rss_feed_thumbnail' ); add_filter( 'the_content_feed', 'rss_feed_thumbnail' );

RSSフィードを確認して、<description><content:encoded>にアイキャッチ画像のimg要素が追加されていることが確認できました。

Feedly一覧表示OK

一覧をクリックして出て来る本文にもアイキャッチ画像が挿入されていていい感じ!\(^o^)/

Feedlyコンテンツアイキャッチ挿入

Feedlyで確認

ここで問題が…。Feedlyに過去に掲載された記事の情報が更新されない…。キャッシュをクリアする方法はないものか。