Last active
April 8, 2022 13:11
-
-
Save gatespace/f3e0339364a637eb24b25aaac0926e37 to your computer and use it in GitHub Desktop.
ogpとtwitter card画像が登録されていない場合はデフォルトに登録した画像で上書き
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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