Last active
August 29, 2015 14:11
-
-
Save kjantzer/b502670ba3a172c18a1d to your computer and use it in GitHub Desktop.
Wordpress: Open Graph Tags
This file contains hidden or 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
| /* | |
| Open Graph Tags | |
| @author Kevin Jantzer | |
| @since 2014-12-13 | |
| orginal code: https://wordpress.org/plugins/facebook-featured-image-and-open-graph-meta-tags/ | |
| Info | |
| - http://ogp.me/ | |
| - https://developers.facebook.com/tools/debug/og/object/ | |
| */ | |
| // Allow for Facebooks's markup language | |
| add_filter('language_attributes', 'add_og_xml_ns'); | |
| function add_og_xml_ns($content) { | |
| return ' xmlns:og="http://ogp.me/ns#" ' . $content; | |
| } | |
| add_filter('language_attributes', 'add_fb_xml_ns'); | |
| function add_fb_xml_ns($content) { | |
| return ' xmlns:fb="https://www.facebook.com/2008/fbml" ' . $content; | |
| } | |
| // Set your Open Graph Meta Tags | |
| function fb_ogmeta_header() { | |
| global $post; | |
| $tags = array( | |
| 'title' => get_the_title(), | |
| 'description' => strip_tags(do_shortcode($post->post_content)), | |
| 'url' => get_the_permalink(), | |
| 'imgs' => get_post_img_urls(), | |
| 'video' => null, | |
| 'type' => (is_single() || is_page()) ? "article" : "website", | |
| 'site' => get_bloginfo('name') | |
| ); | |
| global $youtube_thumbnail; | |
| global $youtube_url; | |
| global $youtube_url_secure; | |
| if( $youtube_thumbnail ) | |
| array_unshift($tags['imgs'], $youtube_thumbnail); | |
| if( $youtube_url ){ | |
| $tags['video'] = array( | |
| 'url' => $youtube_url, | |
| 'url_secure' => $youtube_url_secure, | |
| 'width' => '640', | |
| 'height' => '360' | |
| ); | |
| } | |
| ?> | |
| <!-- Open Graph Meta Tags for Facebook and LinkedIn Sharing !--> | |
| <meta property="og:title" content="<?php echo $tags['title']; ?>"/> | |
| <meta property="og:description" content="<?php echo $tags['description']; ?>" /> | |
| <meta property="og:url" content="<?php echo $tags['url']; ?>"/> | |
| <?php foreach($tags['imgs'] as $img ): ?> | |
| <meta property="og:image" content="<?php echo $img; ?>" /> | |
| <?php endforeach; ?> | |
| <?php if( $tags['video'] ): ?> | |
| <meta property="og:video" content="<?php echo $tags['video']['url']; ?>"> | |
| <meta property="og:video:secure_url" content="<?php echo $tags['video']['url_secure']; ?>"> | |
| <meta property="og:video:width" content="<?php echo $tags['video']['width']; ?>" /> | |
| <meta property="og:video:height" content="<?php echo $tags['video']['height']; ?>" /> | |
| <?php endif; ?> | |
| <meta property="og:type" content="<?php echo $tags['type']; ?>" /> | |
| <meta property="og:site_name" content="<?php echo $tags['site']; ?>"/> | |
| <?php | |
| } | |
| add_action('wp_head', 'fb_ogmeta_header'); | |
| /* | |
| Post Images - returns array of img urls used in post | |
| */ | |
| function get_post_img_urls($post_id=null) { | |
| global $post; | |
| $post_id = $post_id ? $post_id : $post->ID; | |
| $imgs = array(); | |
| $feature_img = wp_get_attachment_image_src(get_post_thumbnail_id ($post_id), 'full'); | |
| if( $feature_img ) | |
| $imgs[] = $feature_img[0]; | |
| $attachments = get_children(array( | |
| 'post_parent' => $post_id, | |
| 'post_type' => 'attachment', | |
| 'post_mime_type' => 'image' | |
| )); | |
| if (is_array($attachments)) foreach ($attachments as $a) | |
| $imgs[] = $a->guid; | |
| return $imgs; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment