Wpにプラグインなしでmeta ogpを設定する

2023年2月12日

いろいろやってみて最終的に現時点ではこれに。
seo.phpとogp.phpに分けてテンプレートheader.phpに読み込むことで実装
ogp.phpにはTwitter Card部分のコードも含まれている。
別記事でも回てますが、indexさせたくないページの記述部分を反映させるためにはfunctions.phpに以下のコードを追加する。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//indexさせないページの設定
if (! function_exists('is_noindex_page')) :
    function is_noindex_page()
    {
        return (is_month()) || // 月のアーカイブページはインデックスに含めない!
        is_category('2') || //カテゴリーarticlsはインデックスに含めない
        is_date() || // 日のアーカイブはインデックスに含めない!
        is_tag() || // タグのアーカイブページをインデックスに含めない!
        is_search() || // 検索結果ページはインデックスに含めない!
        is_404() || // 404ページはインデックスに含めない!
        is_paged() || //分割されたページはインデックスに含めない!
        is_attachment() || //添付ファイルページはインデックスに含めない!
        is_author(); //作成者のアーカイブページはインデックスに含めない!
    }
endif;

ではまずseo.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php if(is_noindex_page()): ?>
<meta name="robots" content="noindex,follow">
<?php endif; ?>
 
<?php if(is_page('front-page') || is_front_page()): ?>
<title>
    <?php wp_title('|', true, 'right'); ?><?php bloginfo('name'); ?>
</title>
<meta name="description" content="<?php bloginfo('description'); ?>" />
 
<?php elseif(is_single()): ?>
<?php if(get_field("originalTitle")): ?>
<title><?php echo the_field('originalTitle'); ?>
</title>
<?php else: ?>
<title><?php wp_title(); ?> | Kureai </title>
<?php endif; ?>
<?php if(get_field("originalDescription")): ?>
<meta name="description" content="<?php echo the_field('originalDescription'); ?>" />
<?php $customfield = get_post_meta($post->ID, 'originalDescription', true); ?>
<?php elseif(empty($customfield) && has_excerpt($post->ID)): ?>
<meta name="description" content="<?php echo original_description(); ?>">
<?php else: ?>
<meta name="description" content="<?php if(have_posts()): ?>
        <?php while(have_posts()): the_post(); ?>
        <?php
         $des = get_the_content();
            $des = strip_tags($des);
            $des = str_replace('&nbsp;', " ", $des);//改行を除去
            $des = str_replace(array("\r\n","\r","\n"), '', $des);//余計な文字列を除去
            $desp = mb_substr($des, 0, 120, "UTF-8");
            echo $desp;
            ?>
        <?php endwhile; ?>
    <?php endif; ?>" />
<?php endif; ?>
 
<?php elseif(is_category()): ?>
<?php if(!is_paged()): ?>
<?php
    $cat_id = get_queried_object()->cat_ID;
    $post_id = 'category_'.$cat_id;
    ?>
<title><?php single_cat_title('', true); ?> |
    <?php bloginfo('name'); ?></title>
<meta name="description" content="<?php
    $cat_id = get_queried_object()->cat_ID;
    $post_id = 'category_'.$cat_id;
    $text = category_description();
    $text = strip_tags($text);
    $text = mb_substr($text, 0, 120, "UTF-8");
    echo $text;
    ?>" />
<?php else: ?>
<title><?php show_page_number(''); ?>ページ目
    <?php single_cat_title('', true); ?> |
    <?php bloginfo('name'); ?></title>
<?php endif; ?>
 
<?php elseif(is_page()): ?>
<?php if(get_field("originalTitle")): ?>
<title><?php echo the_field('originalTitle'); ?>
</title>
<?php else: ?>
<title>
    <?php wp_title('|', true, 'right'); ?><?php bloginfo('name'); ?>
</title>
<?php endif; ?>
<?php if(get_field("originalDescription")): ?>
<meta name="description" content="<?php echo the_field('originalDescription'); ?>" />
<?php else: ?>
<meta name="description" content="<?php bloginfo('description'); ?>" />
<?php endif; ?>
 
<?php elseif(is_search()): ?>
<title>検索結果 | <?php bloginfo('name'); ?></title>
 
<?php elseif(is_404()): ?>
<title>お探しのページはございません |
    <?php bloginfo('name'); ?></title>
 
<!--(7)その他-->
<?php else: ?>
<title>
    <?php wp_title('|', true, 'right'); ?><?php bloginfo('name'); ?>
</title>
<meta name="description" content="<?php bloginfo('description'); ?>" />
<?php endif; ?>
 
<?php
if(!is_404() && !is_search()) {
    echo '<link rel="canonical" href="https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'].'">';
}?>

そしてogp.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!-- OGP -->
<meta property="og:type" content="<?php echo ( is_single() ? 'article' : 'website' ); ?>">
<?php if ( is_home() || is_front_page() ) : ?>
    <meta property="og:title" content="<?php wp_title( '|', true, 'right' ); ?><?php bloginfo( 'name' ); ?>" />
    <meta property="og:description" content="<?php bloginfo( 'description' ); ?>" />
<?php elseif ( is_single() ) : ?>
    <meta property="og:title" content="<?php the_title(); ?>" />
    <?php if ( has_excerpt( $post->ID ) ) : ?>
    <meta property="og:description" content="<?php echo esc_html( get_the_excerpt() ); ?>" />
    <?php else : ?>
            <?php if ( have_posts() ) : ?>
                <?php
                while ( have_posts() ) :
                    the_post();
                    ?>
                    <?php
                        $des  = get_the_content();
                        $des  = strip_tags( $des );
                        $des  = str_replace( ' ', ' ', $des );// 改行を除去!
                        $des  = str_replace( array( "\r\n", "\r", "\n" ), '', $des );// 余計な文字列を除去!
                        $desp = mb_substr( $des, 0, 120, 'UTF-8' );
                    if ( $desp ) {
                        echo '<meta property="og:description" content="' . esc_html( $desp ) . '" />';
                    } else {
                        echo '<meta property="og:description" content="' . esc_html( get_bloginfo( 'description' ) ) . '" />';
                    }
                    ?>
                <?php endwhile; ?>
            <?php endif; ?>
    <?php endif; ?>
<?php elseif ( is_post_type_archive() ) : ?>
    <meta property="og:title" content="<?php wp_title( '|', true, 'right' ); ?><?php bloginfo( 'name' ); ?>" />
    <?php
        $custon_description = get_post_type_object( get_post_type() )->description;
        $custon_description = str_replace( array( "\r\n", "\r", "\n" ), '', $custon_description );
    if ( $custon_description ) {
        echo '<meta property="og:description" content="' . esc_html( $custon_description ) . '" />';
    } else {
        echo '<meta property="og:description" content="' . esc_html( get_bloginfo( 'description' ) ) . '" />';
    };
    ?>
<?php elseif ( is_tax() ) : ?>
    <meta property="og:title" content="<?php single_term_title( '', true ); ?> | <?php bloginfo( 'name' ); ?>" />
    <?php
        $custon_description = strip_tags( term_description() );
        $custon_description = str_replace( array( "\r\n", "\r", "\n" ), '', $custon_description );
    if ( $custon_description ) {
        echo '<meta property="og:description" content="' . esc_html( $custon_description ) . '" />';
    } else {
        echo '<meta property="og:description" content="' . esc_html( get_bloginfo( 'description' ) ) . '" />';
    };
    ?>
<?php elseif ( is_category() ) : ?>
    <?php if ( ! is_paged() ) : ?>
        <meta property="og:title" content="<?php single_cat_title( '', true ); ?> | <?php bloginfo( 'name' ); ?>" />
        <?php
            $cat_id  = get_queried_object()->cat_ID;
            $post_id = 'category_' . $cat_id;
            $text    = category_description();
            $text    = strip_tags( $text );
            $text    = mb_substr( $text, 0, 120, 'UTF-8' );
        if ( $text ) {
            echo '<meta property="og:description" content="' . esc_html( $text ) . '" />';
        } else {
            echo '<meta property="og:description" content="' . esc_html( get_bloginfo( 'description' ) ) . '" />';
        }
        ?>
    <?php else : ?>
        <meta property="og:title" content="<?php show_page_number( '' ); ?>ページ目 <?php single_cat_title( '', true ); ?>" />
    <?php endif; ?>
<?php elseif ( is_page() ) : ?>
    <meta property="og:title" content="<?php the_title(); ?> | <?php bloginfo( 'name' ); ?>" />
        <?php if ( has_excerpt( $post->ID ) ) : ?>
        <meta property="og:description" content="<?php echo esc_html( get_the_excerpt() ); ?>" />
    <?php else : ?>
        <?php if ( have_posts() ) : ?>
            <?php
            while ( have_posts() ) :
                the_post();
                ?>
                <?php
                    $des  = get_the_content();
                    $des  = strip_tags( $des );
                    $des  = str_replace( ' ', ' ', $des );// 改行を除去!
                    $des  = str_replace( array( "\r\n", "\r", "\n" ), '', $des );// 余計な文字列を除去!
                    $desp = mb_substr( $des, 0, 120, 'UTF-8' );
                if ( $desp ) {
                    echo '<meta property="og:description" content="' . esc_html( $desp ) . '" />';
                } else {
                    echo '<meta property="og:description" content="' . esc_html( get_bloginfo( 'description' ) ) . '" />';
                }
                ?>
        <?php endwhile; ?>
    <?php endif; ?>
    <?php endif; ?>
<?php else : ?>
<meta property="og:title" content="<?php bloginfo( 'name' ); ?>" />
<meta property="og:description" content="<?php bloginfo( 'description' ); ?>" />
<?php endif; ?>
<?php
if ( ! is_404() && ! is_search() ) { // 404ページと検索ページでなければ表示!
    echo '<meta property="og:url" content="https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '">';
  echo "\n";
}
?>
<?php
$str            = $post->post_content;
$search_pattern = '/<img.*?src=(["\'])(.+?)\1.*?>/i'; // 投稿にイメージがあるか調べる!
if ( is_single() ) {// 単一記事ページの場合!
    if ( has_post_thumbnail() ) {// 投稿にサムネイルがある場合の処理!
        $image_id = get_post_thumbnail_id();
        $image    = wp_get_attachment_image_src( $image_id, 'full' );
        echo '<meta property="og:image" content="' . esc_url( $image[0] ) . '">';
        echo "\n";
        echo '<meta property="og:image:width" content="' . $image[1] . '">';
        echo "\n";
        echo '<meta property="og:image:height" content="' . $image[2] . '">';
        echo "\n";
    } elseif ( preg_match( $search_pattern, $str, $imgurl ) && ! is_archive() ) {// 投稿にサムネイルは無いが画像がある場合の処理
        echo '<meta property="og:image" content="' . esc_url( $imgurl[2] ) . '">';
        echo "\n";
        echo '<meta property="og:image:width" content="1200">';
        echo "\n";
        echo '<meta property="og:image:height" content="630">';
        echo "\n";
    } else { // 投稿にサムネイルも画像も無い場合の処理!
        $ogp_image = get_template_directory_uri() . '/images/og-image.jpg';
        echo '<meta property="og:image" content="' . esc_url( $ogp_image ) . '">';
        echo "\n";
        echo '<meta property="og:image:width" content="1200">';
        echo "\n";
        echo '<meta property="og:image:height" content="630">';
        echo "\n";
    }
} else { // 単一記事ページページ以外の場合(アーカイブページやホームなど)!
    if ( get_header_image() ) {// ヘッダーイメージがある場合は、ヘッダーイメージを!
        echo '<meta property="og:image" content="' . esc_url( get_header_image() ) . '">';
        echo "\n";
        echo '<meta property="og:image:width" content="1200">';
        echo "\n";
        echo '<meta property="og:image:height" content="630">';
        echo "\n";
    } else { // ヘッダーイメージがない場合は、テーマのスクリーンショット!
        echo '<meta property="og:image" content="' . esc_url( get_template_directory_uri() ) . '/screenshot.png">';
        echo "\n";
        echo '<meta property="og:image:width" content="1200">';
        echo "\n";
        echo '<meta property="og:image:height" content="630">';
        echo "\n";
    }
}
?>
<meta property="og:site_name" content="<?php bloginfo( 'name' ); ?>">
<meta property="og:locale" content="ja_JP" />
<meta property="fb:app_id" content="">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:creator" content="twitterのID">
<meta name="twitter:site" content="twitterのID">