Skip to content

Instantly share code, notes, and snippets.

@danest
Forked from makotokw/header_ogp.php
Created May 6, 2018 12:15
Show Gist options
  • Save danest/0de6aa90d96e8ef9faf43a24de186d2c to your computer and use it in GitHub Desktop.
Save danest/0de6aa90d96e8ef9faf43a24de186d2c to your computer and use it in GitHub Desktop.

Revisions

  1. @makotokw makotokw revised this gist Sep 15, 2013. 1 changed file with 68 additions and 68 deletions.
    136 changes: 68 additions & 68 deletions header_ogp.php
    Original file line number Diff line number Diff line change
    @@ -29,112 +29,112 @@
    //define('WP_OGP_TWITTER_DOMAIN', 'YourDomain.com');

    function ogp_post_description() {
    global $post;
    $description = null;
    if (defined('WP_OGP_POST_DESCRIPTION_KEY')) {
    $description = get_post_meta($post->ID, WP_OGP_POST_DESCRIPTION_KEY, true);
    }
    if (empty($description)) {
    $description = get_the_excerpt();
    }
    return $description;
    global $post;
    $description = null;
    if (defined('WP_OGP_POST_DESCRIPTION_KEY')) {
    $description = get_post_meta($post->ID, WP_OGP_POST_DESCRIPTION_KEY, true);
    }
    if (empty($description)) {
    $description = get_the_excerpt();
    }
    return $description;
    }
    function ogp_post_image() {
    $image = null;
    if ($image_id = get_post_thumbnail_id()) {
    $image = wp_get_attachment_image_src($image_id, WP_OGP_POST_IMAGE_SIZE);
    }
    if (empty($image)) {
    // find first img element
    global $post;
    if (preg_match('/<img[^>]*src\s*=\s*("|\')([^"\']+)("|\')[^>]*>/i', $post->post_content, $matches)) {
    return $matches[2];
    }
    }
    return $image;
    $image = null;
    if ($image_id = get_post_thumbnail_id()) {
    $image = wp_get_attachment_image_src($image_id, WP_OGP_POST_IMAGE_SIZE);
    }
    if (empty($image)) {
    // find first img element
    global $post;
    if (preg_match('/<img[^>]*src\s*=\s*("|\')([^"\']+)("|\')[^>]*>/i', $post->post_content, $matches)) {
    return $matches[2];
    }
    }
    return $image;
    }
    function ogp_post_section() {
    $categories = get_the_category();
    if (count($categories)>0) {
    if ($categories[0]->name != __('Uncategorized')) {
    return $categories[0]->name;
    }
    }
    return false;
    $categories = get_the_category();
    if (count($categories)>0) {
    if ($categories[0]->name != __('Uncategorized')) {
    return $categories[0]->name;
    }
    }
    return false;
    }
    function ogp_post_tag() {
    $tags = get_the_tags();
    $tagnames = array();
    if ($tags) {
    foreach ($tags as $tag) {
    $tagnames[] = $tag->name;
    }
    return implode(',', $tagnames);
    }
    return false;
    $tags = get_the_tags();
    $tagnames = array();
    if ($tags) {
    foreach ($tags as $tag) {
    $tagnames[] = $tag->name;
    }
    return implode(',', $tagnames);
    }
    return false;
    }
    ?>
    <?php if( is_single() || is_page()): ?>
    <?php while(have_posts()): the_post(); ?>
    <meta name="og:title" content="<?php the_title();?>" />
    <meta name="og:type" content="article" />
    <meta property="og:title" content="<?php the_title();?>" />
    <meta property="og:type" content="article" />
    <?php if ($og_image = ogp_post_image()):?>
    <meta name="og:image" content="<?php echo $og_image ?>" />
    <meta property="og:image" content="<?php echo $og_image ?>" />
    <?php else:?>
    <meta name="og:image" content="<?php echo WP_OGP_DEFAULT_IMG ?>" />
    <meta property="og:image" content="<?php echo WP_OGP_DEFAULT_IMG ?>" />
    <?php endif ?>
    <meta name="og:url" content="<?php echo esc_url(get_permalink()); ?>" />
    <meta property="og:url" content="<?php echo esc_url(get_permalink()); ?>" />
    <?php if ($og_description = ogp_post_description()):?>
    <meta name="og:description" content="<?php echo esc_attr($og_description) ?>" />
    <meta property="og:description" content="<?php echo esc_attr($og_description) ?>" />
    <?php else:?>
    <meta name="og:description" content="<?php bloginfo('description'); ?>" />
    <meta property="og:description" content="<?php bloginfo('description'); ?>" />
    <?php endif ?>
    <!-- <meta name="article:author" content="<?php echo get_the_author(); ?>" /> -->
    <meta name="article:published_time" content="<?php echo get_post_time('c') ?>" />
    <meta name="article:modified_time" content="<?php echo get_the_modified_time('c') ?>" />
    <!-- <meta property="article:author" content="<?php echo get_the_author(); ?>" /> -->
    <meta property="article:published_time" content="<?php echo get_post_time('c') ?>" />
    <meta property="article:modified_time" content="<?php echo get_the_modified_time('c') ?>" />
    <?php if ($og_section = ogp_post_section()):?>
    <meta name="article:section" content="<?php echo esc_attr($og_section) ?>" />
    <meta property="article:section" content="<?php echo esc_attr($og_section) ?>" />
    <?php endif ?>
    <?php if ($og_tag = ogp_post_tag()):?>
    <meta name="article:tag" content="<?php echo esc_attr($og_tag) ?>" />
    <meta property="article:tag" content="<?php echo esc_attr($og_tag) ?>" />
    <?php endif ?>
    <meta name="twitter:card" content="summary" />
    <meta name="twitter:title" content="<?php echo mb_strimwidth(get_the_title(), 0, 70, '...'); ?>" />
    <meta property="twitter:card" content="summary" />
    <meta property="twitter:title" content="<?php echo mb_strimwidth(get_the_title(), 0, 70, '...'); ?>" />
    <?php if ($og_description):?>
    <meta name="twitter:description" content="<?php echo mb_strimwidth(esc_attr($og_description), 0, 200); ?>" />
    <meta property="twitter:description" content="<?php echo mb_strimwidth(esc_attr($og_description), 0, 200); ?>" />
    <?php endif; ?>
    <?php if ($og_image):?>
    <meta name="twitter:image:src" content="<?php echo $og_image ?>" />
    <meta property="twitter:image:src" content="<?php echo $og_image ?>" />
    <?php else: ?>
    <meta name="twitter:image:src" content="<?php echo WP_OGP_DEFAULT_IMG ?>" />
    <meta property="twitter:image:src" content="<?php echo WP_OGP_DEFAULT_IMG ?>" />
    <?php endif; ?>
    <?php if (defined('WP_OGP_TWITTER_SITE_USERNAME')): ?>
    <meta name="twitter:site" content="<?php echo WP_OGP_TWITTER_SITE_USERNAME; ?>" />
    <meta property="twitter:site" content="<?php echo WP_OGP_TWITTER_SITE_USERNAME; ?>" />
    <?php endif ?>
    <?php if (defined('WP_OGP_TWITTER_CREATOR_USERNAME')): ?>
    <meta name="twitter:creator" content="<?php echo WP_OGP_TWITTER_CREATOR_USERNAME; ?>" />
    <meta property="twitter:creator" content="<?php echo WP_OGP_TWITTER_CREATOR_USERNAME; ?>" />
    <?php endif ?>
    <?php if (defined('WP_OGP_TWITTER_DOMAIN')): ?>
    <meta name="twitter:domain" content="<?php echo WP_OGP_TWITTER_DOMAIN; ?>" />
    <meta property="twitter:domain" content="<?php echo WP_OGP_TWITTER_DOMAIN; ?>" />
    <?php endif ?>
    <?php endwhile; ?>
    <?php else: ?>
    <meta name="og:title" content="<?php wp_title('|', true, 'right'); ?>" />
    <meta property="og:title" content="<?php wp_title('|', true, 'right'); ?>" />
    <?php if (is_front_page() || is_404()): ?>
    <meta name="og:type" content="blog" />
    <meta name="og:url" content="<?php echo get_bloginfo('url')?>" />
    <meta property="og:type" content="blog" />
    <meta property="og:url" content="<?php echo get_bloginfo('url')?>" />
    <?php else: ?>
    <meta name="og:type" content="article" />
    <meta name="og:url" content="<?php echo get_pagenum_link()?>" />
    <meta property="og:type" content="article" />
    <meta property="og:url" content="<?php echo get_pagenum_link()?>" />
    <?php endif ?>
    <meta name="og:image" content="<?php echo WP_OGP_DEFAULT_IMG?>" />
    <meta name="og:description" content="<?php bloginfo('description'); ?>" />
    <meta property="og:image" content="<?php echo WP_OGP_DEFAULT_IMG?>" />
    <meta property="og:description" content="<?php bloginfo('description'); ?>" />
    <?php endif ?>
    <meta name="og:locale" content="<?php echo WP_OGP_LOCALE?>" />
    <meta name="og:site_name" content="<?php bloginfo('name'); ?>" />
    <meta property="og:locale" content="<?php echo WP_OGP_LOCALE?>" />
    <meta property="og:site_name" content="<?php bloginfo('name'); ?>" />
    <?php if (defined('WP_OGP_FB_APPID')): ?>
    <meta name="fb:app_id" content="<?php echo WP_OGP_FB_APPID; ?>" />
    <meta property="fb:app_id" content="<?php echo WP_OGP_FB_APPID; ?>" />
    <?php endif ?>
    <?php if (defined('WP_OGP_FB_ADMINS')): ?>
    <meta name="fb:admins" content="<?php echo WP_OGP_FB_ADMINS; ?>" />
    <meta property="fb:admins" content="<?php echo WP_OGP_FB_ADMINS; ?>" />
    <?php endif ?>
  2. @makotokw makotokw revised this gist Jul 27, 2013. 1 changed file with 49 additions and 25 deletions.
    74 changes: 49 additions & 25 deletions header_ogp.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php
    /**
    * Open Graph protocol for WordPress
    * @version 0.9.1
    * @version 0.9.2
    * @author makoto_kw
    * @link https://gist.github.com/3399585
    */
    @@ -23,6 +23,11 @@
    //define('WP_OGP_FB_APPID', 'your-appid');
    //define('WP_OGP_FB_ADMINS', 'your-admins');

    // Twitter Usernames
    //define('WP_OGP_TWITTER_SITE_USERNAME', '@site_username');
    //define('WP_OGP_TWITTER_CREATOR_USERNAME', '@creator_username');
    //define('WP_OGP_TWITTER_DOMAIN', 'YourDomain.com');

    function ogp_post_description() {
    global $post;
    $description = null;
    @@ -71,46 +76,65 @@ function ogp_post_tag() {
    ?>
    <?php if( is_single() || is_page()): ?>
    <?php while(have_posts()): the_post(); ?>
    <meta property="og:title" content="<?php the_title();?>">
    <meta property="og:type" content="article" />
    <meta name="og:title" content="<?php the_title();?>" />
    <meta name="og:type" content="article" />
    <?php if ($og_image = ogp_post_image()):?>
    <meta property="og:image" content="<?php echo $og_image ?>" />
    <meta name="og:image" content="<?php echo $og_image ?>" />
    <?php else:?>
    <meta property="og:image" content="<?php echo WP_OGP_DEFAULT_IMG ?>" />
    <meta name="og:image" content="<?php echo WP_OGP_DEFAULT_IMG ?>" />
    <?php endif ?>
    <meta property="og:url" content="<?php echo esc_url(get_permalink()); ?>" />
    <meta name="og:url" content="<?php echo esc_url(get_permalink()); ?>" />
    <?php if ($og_description = ogp_post_description()):?>
    <meta property="og:description" content="<?php echo esc_attr($og_description) ?>" />
    <meta name="og:description" content="<?php echo esc_attr($og_description) ?>" />
    <?php else:?>
    <meta property="og:description" content="<?php bloginfo('description'); ?>" />
    <meta name="og:description" content="<?php bloginfo('description'); ?>" />
    <?php endif ?>
    <!-- <meta property="article:author" content="<?php echo get_the_author(); ?>" /> -->
    <meta property="article:published_time" content="<?php echo get_post_time('c') ?>" />
    <meta property="article:modified_time" content="<?php echo get_the_modified_time('c') ?>" />
    <!-- <meta name="article:author" content="<?php echo get_the_author(); ?>" /> -->
    <meta name="article:published_time" content="<?php echo get_post_time('c') ?>" />
    <meta name="article:modified_time" content="<?php echo get_the_modified_time('c') ?>" />
    <?php if ($og_section = ogp_post_section()):?>
    <meta property="article:section" content="<?php echo esc_attr($og_section) ?>" />
    <meta name="article:section" content="<?php echo esc_attr($og_section) ?>" />
    <?php endif ?>
    <?php if ($og_tag = ogp_post_tag()):?>
    <meta property="article:tag" content="<?php echo esc_attr($og_tag) ?>" />
    <meta name="article:tag" content="<?php echo esc_attr($og_tag) ?>" />
    <?php endif ?>
    <meta name="twitter:card" content="summary" />
    <meta name="twitter:title" content="<?php echo mb_strimwidth(get_the_title(), 0, 70, '...'); ?>" />
    <?php if ($og_description):?>
    <meta name="twitter:description" content="<?php echo mb_strimwidth(esc_attr($og_description), 0, 200); ?>" />
    <?php endif; ?>
    <?php if ($og_image):?>
    <meta name="twitter:image:src" content="<?php echo $og_image ?>" />
    <?php else: ?>
    <meta name="twitter:image:src" content="<?php echo WP_OGP_DEFAULT_IMG ?>" />
    <?php endif; ?>
    <?php if (defined('WP_OGP_TWITTER_SITE_USERNAME')): ?>
    <meta name="twitter:site" content="<?php echo WP_OGP_TWITTER_SITE_USERNAME; ?>" />
    <?php endif ?>
    <?php if (defined('WP_OGP_TWITTER_CREATOR_USERNAME')): ?>
    <meta name="twitter:creator" content="<?php echo WP_OGP_TWITTER_CREATOR_USERNAME; ?>" />
    <?php endif ?>
    <?php if (defined('WP_OGP_TWITTER_DOMAIN')): ?>
    <meta name="twitter:domain" content="<?php echo WP_OGP_TWITTER_DOMAIN; ?>" />
    <?php endif ?>
    <?php endwhile; ?>
    <?php else: ?>
    <meta property="og:title" content="<?php wp_title('|', true, 'right'); ?>" />
    <meta name="og:title" content="<?php wp_title('|', true, 'right'); ?>" />
    <?php if (is_front_page() || is_404()): ?>
    <meta property="og:type" content="blog" />
    <meta property="og:url" content="<?php echo get_bloginfo('url')?>" />
    <meta name="og:type" content="blog" />
    <meta name="og:url" content="<?php echo get_bloginfo('url')?>" />
    <?php else: ?>
    <meta property="og:type" content="article" />
    <meta property="og:url" content="<?php echo get_pagenum_link()?>" />
    <meta name="og:type" content="article" />
    <meta name="og:url" content="<?php echo get_pagenum_link()?>" />
    <?php endif ?>
    <meta property="og:image" content="<?php echo WP_OGP_DEFAULT_IMG?>" />
    <meta property="og:description" content="<?php bloginfo('description'); ?>" />
    <meta name="og:image" content="<?php echo WP_OGP_DEFAULT_IMG?>" />
    <meta name="og:description" content="<?php bloginfo('description'); ?>" />
    <?php endif ?>
    <meta property="og:locale" content="<?php echo WP_OGP_LOCALE?>" />
    <meta property="og:site_name" content="<?php bloginfo('name'); ?>" />
    <meta name="og:locale" content="<?php echo WP_OGP_LOCALE?>" />
    <meta name="og:site_name" content="<?php bloginfo('name'); ?>" />
    <?php if (defined('WP_OGP_FB_APPID')): ?>
    <meta property="fb:app_id" content="<?php echo WP_OGP_FB_APPID; ?>">
    <meta name="fb:app_id" content="<?php echo WP_OGP_FB_APPID; ?>" />
    <?php endif ?>
    <?php if (defined('WP_OGP_FB_ADMINS')): ?>
    <meta property="fb:admins" content="<?php echo WP_OGP_FB_ADMINS; ?>">
    <?php endif ?>
    <meta name="fb:admins" content="<?php echo WP_OGP_FB_ADMINS; ?>" />
    <?php endif ?>
  3. @makotokw makotokw revised this gist Jan 27, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions header_ogp.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php
    /**
    * Open Graph protocol for WordPress
    * @version 0.9
    * @version 0.9.1
    * @author makoto_kw
    * @link https://gist.github.com/3399585
    */
    @@ -78,7 +78,7 @@ function ogp_post_tag() {
    <?php else:?>
    <meta property="og:image" content="<?php echo WP_OGP_DEFAULT_IMG ?>" />
    <?php endif ?>
    <meta property="og:url" content="<?php echo clean_url(get_permalink()); ?>" />
    <meta property="og:url" content="<?php echo esc_url(get_permalink()); ?>" />
    <?php if ($og_description = ogp_post_description()):?>
    <meta property="og:description" content="<?php echo esc_attr($og_description) ?>" />
    <?php else:?>
  4. @makotokw makotokw revised this gist Aug 28, 2012. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions header_ogp.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    <?php
    /**
    * Open Graph protocol for WordPress
    * @version 0.9
    * @author makoto_kw
    * @link https://gist.github.com/3399585
    */
    // key into custom fields for description. Default is for All in One SEO Pack
    define('WP_OGP_POST_DESCRIPTION_KEY', '_aioseop_description');
    @@ -38,7 +40,7 @@ function ogp_post_image() {
    $image = wp_get_attachment_image_src($image_id, WP_OGP_POST_IMAGE_SIZE);
    }
    if (empty($image)) {
    // imgタグを検索
    // find first img element
    global $post;
    if (preg_match('/<img[^>]*src\s*=\s*("|\')([^"\']+)("|\')[^>]*>/i', $post->post_content, $matches)) {
    return $matches[2];
    @@ -94,7 +96,7 @@ function ogp_post_tag() {
    <?php endwhile; ?>
    <?php else: ?>
    <meta property="og:title" content="<?php wp_title('|', true, 'right'); ?>" />
    <?php if (is_front_page()): ?>
    <?php if (is_front_page() || is_404()): ?>
    <meta property="og:type" content="blog" />
    <meta property="og:url" content="<?php echo get_bloginfo('url')?>" />
    <?php else: ?>
  5. @makotokw makotokw revised this gist Aug 20, 2012. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions header_ogp.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,13 @@
    /**
    * Open Graph protocol for WordPress
    * @version 0.9
    * @author makoto_kw
    */
    // key into custom fields for description. Default is for All in One SEO Pack
    define('WP_OGP_POST_DESCRIPTION_KEY', '_aioseop_description');

    // size of Feature Image
    // http://codex.wordpress.org/Post_Thumbnails
    define('WP_OGP_POST_IMAGE_SIZE', 'medium');

    // default Image Url
  6. @invalid-email-address Anonymous created this gist Aug 20, 2012.
    108 changes: 108 additions & 0 deletions header_ogp.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,108 @@
    // key into custom fields for description. Default is for All in One SEO Pack
    define('WP_OGP_POST_DESCRIPTION_KEY', '_aioseop_description');

    // size of Feature Image
    define('WP_OGP_POST_IMAGE_SIZE', 'medium');

    // default Image Url
    define('WP_OGP_DEFAULT_IMG', bloginfo('template_url').'/images/ogp_default.jpg');

    // locale
    define('WP_OGP_LOCALE', 'ja_JP');

    // Facebook AppId and username
    // http://developers.facebook.com/docs/opengraphprotocol/
    //define('WP_OGP_FB_APPID', 'your-appid');
    //define('WP_OGP_FB_ADMINS', 'your-admins');

    function ogp_post_description() {
    global $post;
    $description = null;
    if (defined('WP_OGP_POST_DESCRIPTION_KEY')) {
    $description = get_post_meta($post->ID, WP_OGP_POST_DESCRIPTION_KEY, true);
    }
    if (empty($description)) {
    $description = get_the_excerpt();
    }
    return $description;
    }
    function ogp_post_image() {
    $image = null;
    if ($image_id = get_post_thumbnail_id()) {
    $image = wp_get_attachment_image_src($image_id, WP_OGP_POST_IMAGE_SIZE);
    }
    if (empty($image)) {
    // imgタグを検索
    global $post;
    if (preg_match('/<img[^>]*src\s*=\s*("|\')([^"\']+)("|\')[^>]*>/i', $post->post_content, $matches)) {
    return $matches[2];
    }
    }
    return $image;
    }
    function ogp_post_section() {
    $categories = get_the_category();
    if (count($categories)>0) {
    if ($categories[0]->name != __('Uncategorized')) {
    return $categories[0]->name;
    }
    }
    return false;
    }
    function ogp_post_tag() {
    $tags = get_the_tags();
    $tagnames = array();
    if ($tags) {
    foreach ($tags as $tag) {
    $tagnames[] = $tag->name;
    }
    return implode(',', $tagnames);
    }
    return false;
    }
    ?>
    <?php if( is_single() || is_page()): ?>
    <?php while(have_posts()): the_post(); ?>
    <meta property="og:title" content="<?php the_title();?>">
    <meta property="og:type" content="article" />
    <?php if ($og_image = ogp_post_image()):?>
    <meta property="og:image" content="<?php echo $og_image ?>" />
    <?php else:?>
    <meta property="og:image" content="<?php echo WP_OGP_DEFAULT_IMG ?>" />
    <?php endif ?>
    <meta property="og:url" content="<?php echo clean_url(get_permalink()); ?>" />
    <?php if ($og_description = ogp_post_description()):?>
    <meta property="og:description" content="<?php echo esc_attr($og_description) ?>" />
    <?php else:?>
    <meta property="og:description" content="<?php bloginfo('description'); ?>" />
    <?php endif ?>
    <!-- <meta property="article:author" content="<?php echo get_the_author(); ?>" /> -->
    <meta property="article:published_time" content="<?php echo get_post_time('c') ?>" />
    <meta property="article:modified_time" content="<?php echo get_the_modified_time('c') ?>" />
    <?php if ($og_section = ogp_post_section()):?>
    <meta property="article:section" content="<?php echo esc_attr($og_section) ?>" />
    <?php endif ?>
    <?php if ($og_tag = ogp_post_tag()):?>
    <meta property="article:tag" content="<?php echo esc_attr($og_tag) ?>" />
    <?php endif ?>
    <?php endwhile; ?>
    <?php else: ?>
    <meta property="og:title" content="<?php wp_title('|', true, 'right'); ?>" />
    <?php if (is_front_page()): ?>
    <meta property="og:type" content="blog" />
    <meta property="og:url" content="<?php echo get_bloginfo('url')?>" />
    <?php else: ?>
    <meta property="og:type" content="article" />
    <meta property="og:url" content="<?php echo get_pagenum_link()?>" />
    <?php endif ?>
    <meta property="og:image" content="<?php echo WP_OGP_DEFAULT_IMG?>" />
    <meta property="og:description" content="<?php bloginfo('description'); ?>" />
    <?php endif ?>
    <meta property="og:locale" content="<?php echo WP_OGP_LOCALE?>" />
    <meta property="og:site_name" content="<?php bloginfo('name'); ?>" />
    <?php if (defined('WP_OGP_FB_APPID')): ?>
    <meta property="fb:app_id" content="<?php echo WP_OGP_FB_APPID; ?>">
    <?php endif ?>
    <?php if (defined('WP_OGP_FB_ADMINS')): ?>
    <meta property="fb:admins" content="<?php echo WP_OGP_FB_ADMINS; ?>">
    <?php endif ?>