Last active
March 22, 2018 02:18
-
-
Save kobaatsu/4f97d5bea01971e5d774 to your computer and use it in GitHub Desktop.
[OGP for Wordpress] 色々改変の余地あり #wordpress
This file contains 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 | |
| $name_site = get_bloginfo('name'); | |
| $url_parsed = parse_url(get_bloginfo('url')); | |
| $domain_site = $url_parsed['host']; | |
| $url_template = get_bloginfo('template_url'); | |
| $title_page = ''; | |
| if(!is_front_page()){ | |
//- 前に謎のスペースが入るのでtrimする | |
| $title_page .= trim(wp_title('', false)) . ' | '; | |
| } | |
| $title_page .= get_bloginfo('name'); | |
| if(is_front_page()){ | |
| $type_page = 'website'; | |
| } else { | |
| $type_page = 'article'; | |
| } | |
| $url_page = get_bloginfo('url').$_SERVER['REQUEST_URI']; | |
| if(is_front_page()){ | |
| $description_page = get_bloginfo('description'); | |
| } elseif(is_archive()) { | |
| if(is_category()) { | |
//- カテゴリーの説明を取得 | |
| $description_page = strip_tags(category_description()); | |
| } else { | |
//- カスタムポストの説明を取得 | |
| $description_page = get_post_type_object(get_post_type())->description; | |
| } | |
| } else { | |
| global $post; | |
| $length = 100; | |
| $description_page = strip_tags($post->post_content); | |
| $description_page = mb_substr($description_page, 0, $length); | |
| $description_page .= '...'; | |
| $description_page = str_replace(array("\r\n","\r","\n"), '', $description_page); | |
| $description_page = preg_replace('/\s+/', ' ', $description_page); | |
| } | |
| $description_page = str_replace('"', '"', $description_page); | |
| ?> | |
//- サイトにあわせて書き直す | |
- var iconFacebook = "/image/icon_facebook-OGP.png"; | |
- var iconTwitter = "/image/icon_twitter-card.png"; | |
- var idFacebookApp = "000000000000"; | |
- var idTwitter = "twitterAccount"; | |
- var ogp = {"og:site_name":"<?php echo $name_site; ?>", "og:title":"<?php echo $title_page; ?>", "og:type":"<?php echo $type_page; ?>", "og:url":"<?php echo $url_page; ?>", "og:description":"<?php echo $description_page; ?>", "twitter:card":"summary", "twitter:title":"<?php echo $title_page; ?>", "twitter:url":"<?php echo $url_page; ?>", "twitter:domain":"<?php echo $domain_site; ?>", "twitter:description":"<?php echo $description_page; ?>"} | |
each val, key in ogp | |
meta(property!=key, content!=val) | |
if iconFacebook | |
meta(property="og:image", content!="<?php echo $url_template; ?>"+iconFacebook) | |
if iconTwitter | |
meta(property="twitter:image", content!="<?php echo $url_template; ?>"+iconTwitter) | |
if idFacebookApp | |
meta(property="fb:app_id", content=idFacebookApp) | |
if idTwitter | |
meta(property="twitter:site", content="@"+idTwitter) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment