0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Postieプラグインでデフォルトのアイキャッチ画像を設定する

Last updated at Posted at 2021-06-25

やりたいこと

WordPressPostie プラグインで、デフォルトのアイキャッチ画像を設定する.

詳細

WordPress でメールによる投稿を可能にするプラグイン Postie.

メールに添付した最初の画像が自動的にアイキャッチ画像(Featured Image)になるように設定できるが, 画像が添付されていない場合にはアイキャッチ画像無しになる.

この場合にはあらかじめ指定したデフォルトの画像がアイキャッチになるようにしたいと思い, Default Featured Image を試してみたら, 画像が添付されていてもデフォルトの画像になってしまった.

解決策

Postie の拡張フィルター機能を使えば何とかなりそう. ということで, WordPress Codex を調べて以下の関数を発見.

Postie のドキュメントに従い "wp-content" ディレクトリに "filterPostie.php" というファイルを作成し, 以下を記述.

filterPostie.php
<?php 
function my_postie_post_function($post) {
  if ( !has_post_thumbnail($post['ID']) ) {
    set_post_thumbnail( $post['ID'], 1298 );
  }
  return $post;
}

add_filter('postie_post_after', 'my_postie_post_function');
?>

投稿後に処理されるフィルタ postie_post_after を定義し, アイキャッチが設定されていない場合のみ, デフォルトの画像を設定している.

set_post_thumbnail() の第2引数(上記の例では "1298")には WordPress のメディアライブラリに登録されている画像のIDを指定する.

画像IDの調べ方は 【WP REST API解説】 を参照.

これでやりたいことができた.

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?