Last active
November 28, 2018 16:41
-
-
Save M66B/e2c212b928fa4ab6b7a3835fa035da46 to your computer and use it in GitHub Desktop.
WordPress Simple OGP
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 | |
/* | |
Plugin Name: Simple OGP embed | |
Plugin URI: https://www.faircode.eu/ | |
Description: Simple OGP embed | |
Version: 0.1 | |
Author: Marcel Bokhorst | |
Author URI: https://www.faircode.eu/ | |
*/ | |
/* | |
Copyright 2016 Marcel Bokhorst | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
add_action('wp_head', 'm66b_wp_head'); | |
function m66b_wp_head() { | |
if (is_single() || is_page()) { | |
// Get blog info | |
$charset = get_bloginfo('charset'); | |
$locale = get_bloginfo('language'); | |
$locale = (empty($locale) ? 'en_US' : str_replace('-', '_', $locale)); | |
if (strlen($locale) == 2) | |
$locale = strtolower($locale) . '_' . strtoupper($locale); | |
$title = html_entity_decode(get_bloginfo('title'), ENT_QUOTES, $charset); | |
// Get post info | |
$post = get_post(); | |
$post_title = html_entity_decode(get_the_title($post->ID), ENT_QUOTES, $charset); | |
// Get post content | |
$content = $post->post_content; | |
if (function_exists('m66b_handle_gphotos_embed')) | |
$content = preg_replace_callback('#https\://goo\.gl/photos/(.+)#i', function ($matches) { | |
return m66b_handle_gphotos_embed($matches, null, trim($matches[0]), null); | |
}, $content); | |
// Get post excerpt | |
$excerpt = trim(strip_tags(strip_shortcodes($content))); | |
$words = explode(' ', $excerpt, 55 + 1); | |
if (count($words) > 55) { | |
array_pop($words); | |
array_push($words, '…'); | |
$excerpt = implode(' ', $words); | |
} | |
// Get featured image | |
$picture = null; | |
if (current_theme_supports('post-thumbnails')) { | |
$featured_image_id = get_post_thumbnail_id($post->ID); | |
if ($featured_image_id) { | |
$src = wp_get_attachment_image_src($featured_image_id, 'medium'); | |
if ($src) | |
$picture = $src[0]; | |
} | |
} | |
// Get first picture in post | |
if ($picture == null) { | |
if (preg_match('/< *img[^>]*src *= *["\']([^"\']*)["\']/i', $content, $matches)) | |
$picture = $matches[1]; | |
if (strpos($picture_post, 'data:') === 0 && strpos($picture_post, 'base64') > 0) | |
$picture = null; | |
} | |
// Render ogp meta tags | |
echo '<!-- Start M66B OGP -->' . PHP_EOL; | |
echo '<meta property="og:title" content="' . esc_attr($post_title) . '" />' . PHP_EOL; | |
echo '<meta property="og:type" content="article" />' . PHP_EOL; | |
if ($picture) | |
echo '<meta property="og:image" content="' . esc_attr($picture) . '" />' . PHP_EOL; | |
echo '<meta property="og:url" content="' . esc_attr(get_permalink($post->ID)) . '" />' . PHP_EOL; | |
echo '<meta property="og:site_name" content="' . esc_attr($title) . '" />' . PHP_EOL; | |
echo '<meta property="og:description" content="' . esc_attr($excerpt) . '" />' . PHP_EOL; | |
echo '<meta property="og:locale" content="' . esc_attr($locale) . '" />' . PHP_EOL; | |
echo '<!-- End M66B OGP -->' . PHP_EOL; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment