Skip to content

Instantly share code, notes, and snippets.

@gatespace
Last active April 8, 2022 13:11
Show Gist options
  • Save gatespace/f3e0339364a637eb24b25aaac0926e37 to your computer and use it in GitHub Desktop.
Save gatespace/f3e0339364a637eb24b25aaac0926e37 to your computer and use it in GitHub Desktop.
ogpとtwitter card画像が登録されていない場合はデフォルトに登録した画像で上書き
<?php
//ogpとtwitter card画像が登録されていない場合はデフォルトに登録した画像で上書き
function override_ogp_img($img){
$og_image_flag = true;
if ( is_single()) {
$post_id = get_the_id();
$og_image = get_post_meta( $post_id, '_yoast_wpseo_opengraph-image', true ); // yoast 取得
if ( empty( $og_image ) ) { // yoastの投稿の設定がない
$og_image_flag = false;
}
if ( has_post_thumbnail( $post_id ) ) { // アイキャッチ画像がある
$og_image_flag = true;
}
}
// フラグfalse
if ( $og_image_flag === false ){
$img = "https://image.paypay.ne.jp/page/common/images/bnr_ogp_01.png";
}
return $img;
}
add_filter( 'wpseo_opengraph_image', 'override_ogp_img', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment