| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Open Graph |
| 4 |
Plugin URI: http://wordpress.org/extend/plugins/opengraph |
| 5 |
Description: Adds Open Graph metadata to your pages |
| 6 |
Author: Will Norris |
| 7 |
Author URI: http://willnorris.com/ |
| 8 |
Version: 1.3 |
| 9 |
License: Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html) |
| 10 |
Text Domain: opengraph |
| 11 |
*/ |
| 12 |
|
| 13 |
|
| 14 |
define('OPENGRAPH_PREFIX_URI', 'http://ogp.me/ns#'); |
| 15 |
$opengraph_prefix_set = false; |
| 16 |
|
| 17 |
|
| 18 |
/** |
| 19 |
* Add Open Graph XML prefix to <html> element. |
| 20 |
* |
| 21 |
* @uses apply_filters calls 'opengraph_prefixes' filter on RDFa prefix array |
| 22 |
*/ |
| 23 |
function opengraph_add_prefix( $output ) { |
| 24 |
$prefixes = array( |
| 25 |
'og' => OPENGRAPH_PREFIX_URI |
| 26 |
); |
| 27 |
$prefixes = apply_filters('opengraph_prefixes', $prefixes); |
| 28 |
|
| 29 |
$prefix_str = ''; |
| 30 |
foreach( $prefixes as $k => $v ) { |
| 31 |
$prefix_str .= $k . ': ' . $v . ' '; |
| 32 |
} |
| 33 |
$prefix_str = trim($prefix_str); |
| 34 |
|
| 35 |
if (preg_match('/(prefix\s*=\s*[\"|\'])/i', $output)) { |
| 36 |
$output = preg_replace('/(prefix\s*=\s*[\"|\'])/i', '${1}' . $prefix_str, $output); |
| 37 |
} else { |
| 38 |
$output .= ' prefix="' . $prefix_str . '"'; |
| 39 |
} |
| 40 |
return $output; |
| 41 |
} |
| 42 |
add_filter('language_attributes', 'opengraph_add_prefix'); |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Get the Open Graph metadata for the current page. |
| 47 |
* |
| 48 |
* @uses apply_filters() Calls 'opengraph_{$name}' for each property name |
| 49 |
* @uses apply_filters() Calls 'opengraph_metadata' before returning metadata array |
| 50 |
*/ |
| 51 |
function opengraph_metadata() { |
| 52 |
$metadata = array(); |
| 53 |
|
| 54 |
// defualt properties defined at http://ogp.me/ |
| 55 |
$properties = array( |
| 56 |
// required |
| 57 |
'title', 'type', 'image', 'url', |
| 58 |
|
| 59 |
// optional |
| 60 |
'audio', 'description', 'determiner', 'locale', 'site_name', 'video', |
| 61 |
); |
| 62 |
|
| 63 |
foreach ($properties as $property) { |
| 64 |
$filter = 'opengraph_' . $property; |
| 65 |
$metadata["og:$property"] = apply_filters($filter, ''); |
| 66 |
} |
| 67 |
return apply_filters('opengraph_metadata', $metadata); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* Register filters for default Open Graph metadata. |
| 73 |
*/ |
| 74 |
function opengraph_default_metadata() { |
| 75 |
add_filter('opengraph_title', 'opengraph_default_title', 5); |
| 76 |
add_filter('opengraph_type', 'opengraph_default_type', 5); |
| 77 |
add_filter('opengraph_image', 'opengraph_default_image', 5); |
| 78 |
add_filter('opengraph_url', 'opengraph_default_url', 5); |
| 79 |
|
| 80 |
add_filter('opengraph_description', 'opengraph_default_description', 5); |
| 81 |
add_filter('opengraph_locale', 'opengraph_default_locale', 5); |
| 82 |
add_filter('opengraph_site_name', 'opengraph_default_sitename', 5); |
| 83 |
} |
| 84 |
add_filter('wp', 'opengraph_default_metadata'); |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Default title property, using the page title. |
| 89 |
*/ |
| 90 |
function opengraph_default_title( $title ) { |
| 91 |
if ( is_singular() && empty($title) ) { |
| 92 |
global $post; |
| 93 |
$title = $post->post_title; |
| 94 |
} |
| 95 |
return $title; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* Default type property. |
| 101 |
*/ |
| 102 |
function opengraph_default_type( $type ) { |
| 103 |
if ( is_singular( array('post', 'page', 'aside', 'status') ) && empty($type) ) { |
| 104 |
$type = 'article'; |
| 105 |
} elseif ( empty($type) ) { |
| 106 |
$type = 'blog'; |
| 107 |
} |
| 108 |
return $type; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Default image property, using the post-thumbnail. |
| 114 |
*/ |
| 115 |
function opengraph_default_image( $image ) { |
| 116 |
global $post; |
| 117 |
if ( function_exists('has_post_thumbnail') ) { |
| 118 |
if ( is_singular() && empty($image) && has_post_thumbnail($post->ID) ) { |
| 119 |
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail'); |
| 120 |
if ($thumbnail) { |
| 121 |
$image = $thumbnail[0]; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
return $image; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
/** |
| 130 |
* Default url property, using the permalink for the page. |
| 131 |
*/ |
| 132 |
function opengraph_default_url( $url ) { |
| 133 |
if ( is_singular() && empty($url) ) $url = get_permalink(); |
| 134 |
return $url; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Default site_name property, using the bloginfo name. |
| 140 |
*/ |
| 141 |
function opengraph_default_sitename( $name ) { |
| 142 |
if ( empty($name) ) $name = get_bloginfo('name'); |
| 143 |
return $name; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* Default description property, using the bloginfo description. |
| 149 |
*/ |
| 150 |
function opengraph_default_description( $description ) { |
| 151 |
if ( is_singular() && empty($description) ) { |
| 152 |
if ( has_excerpt() ) { |
| 153 |
$description = get_the_excerpt(); |
| 154 |
} else { |
| 155 |
global $post; |
| 156 |
$description = wp_trim_words( strip_shortcodes($post->post_content), 25, '...' ); |
| 157 |
} |
| 158 |
} elseif ( empty($description) ) { |
| 159 |
$description = get_bloginfo('description'); |
| 160 |
} |
| 161 |
return $description; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* Default locale property, using the WordPress locale. |
| 167 |
*/ |
| 168 |
function opengraph_default_locale( $locale ) { |
| 169 |
if ( empty($locale) ) $locale = get_locale(); |
| 170 |
return $locale; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
/** |
| 175 |
* Output Open Graph <meta> tags in the page header. |
| 176 |
*/ |
| 177 |
function opengraph_meta_tags() { |
| 178 |
$metadata = opengraph_metadata(); |
| 179 |
foreach ( $metadata as $key => $value ) { |
| 180 |
if ( empty($key) || empty($value) ) continue; |
| 181 |
if ( is_array( $value ) ) { |
| 182 |
foreach ( $value as $v ) { |
| 183 |
echo '<meta property="' . esc_attr($key) . '" content="' . esc_attr($v) . '" />' . "\n"; |
| 184 |
} |
| 185 |
} else { |
| 186 |
echo '<meta property="' . esc_attr($key) . '" content="' . esc_attr($value) . '" />' . "\n"; |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
add_action('wp_head', 'opengraph_meta_tags'); |
| 191 |
|
| 192 |
|