Skip to content

Instantly share code, notes, and snippets.

@edy4c7
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save edy4c7/64a1b6f63ef4dffad5f3 to your computer and use it in GitHub Desktop.

Select an option

Save edy4c7/64a1b6f63ef4dffad5f3 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: SimpleOGP
Description: OGP設定出力するだけの簡単なお仕事。
Author: edy
Version: 1.0
*/
class Open_Graph{
const TAGS = "<meta property=\"og:type\" content=\"%s\">\n
<meta property=\"og:title\" content=\"%s\">\n
<meta property=\"og:image\" content=\"%s\">\n
<meta property=\"og:url\" content=\"%s\">\n
<meta property=\"og:description\" content=\"%s\">\n
<meta property=\"og:site_name\" content=\"%s\">\n";
private $type,$title,$image,$url,$description,$site_name;
function __construct($default_image){
$this->type = is_front_page() ? 'website' : 'article';
$this->title = wp_title('|',false,'right') . get_bloginfo('name');
$this->url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$this->site_name = get_bloginfo('name');
if(is_single() && have_posts()){
//個別記事ページ
while (have_posts()){
the_post();
//サムネイルが設定されている場合、それをOGP画像として使う。
//設定されていない場合は$default_imageを使う。
$this->image = has_post_thumbnail() ?
wp_get_attachment_url(get_post_thumbnail_id()) : $default_image;
//抜粋の最初の100文字をdescriptionとして使う。
$this->description = mb_substr(get_the_excerpt(),0,100);
if (mb_strlen($this->description) >= 100) {
$this->description . '...';
}
}
}
else{
//個別記事ページ以外
$this->image = $default_image;
$this->description = get_bloginfo('description');
}
}
public function print_html_tags(){
printf(
self::TAGS,
$this->type,
$this->title,
$this->image,
$this->url,
$this->description,
$this->site_name
);
}
}
function print_open_graph(){
$ogp = new Open_Graph($tmp_dir . 'imgs/logow.png');
$ogp->print_html_tags();
}
add_action('wp_head','print_open_graph');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment