PluginProbe
Open Graph / 1.2
Open Graph v1.2
trunk 1.1 1.10.0 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.12.1 1.12.2 1.2 1.3 1.4 1.5 1.5.1 1.6 1.7.0 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 2.0.0 2.0.1 2.0.2
opengraph / opengraph.php

opengraph.php in Open Graph 1.2, at opengraph.php

170 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.2
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 function opengraph_add_prefix( $output ) {
22 global $opengraph_prefix_set;
23 $opengraph_prefix_set = true;
24
25 if (preg_match('/(prefix\s*=\s*[\"|\'])/i', $output)) {
26 $output = preg_replace('/(prefix\s*=\s*[\"|\'])/i', '${1}og: ' . esc_attr(OPENGRAPH_PREFIX_URI) . ' ', $output);
27 } else {
28 $output .= ' prefix="og: ' . esc_attr(OPENGRAPH_PREFIX_URI) . '"';
29 }
30 return $output;
31 }
32 add_filter('language_attributes', 'opengraph_add_prefix');
33
34
35 /**
36 * Get the Open Graph metadata for the current page.
37 *
38 * @uses apply_filters() Calls 'opengraph_{$name}' for each property name
39 * @uses apply_filters() Calls 'opengraph_metadata' before returning metadata array
40 */
41 function opengraph_metadata() {
42 $metadata = array();
43
44 // defualt properties defined at http://opengraphprotocol.org/
45 $properties = array(
46 // required
47 'title', 'type', 'image', 'url',
48
49 // optional
50 'site_name', 'description',
51
52 // location
53 'longitude', 'latitude', 'street-address', 'locality', 'region',
54 'postal-code', 'country-name',
55
56 // contact
57 'email', 'phone_number', 'fax_number',
58 );
59
60 foreach ($properties as $property) {
61 $filter = 'opengraph_' . $property;
62 $metadata["og:$property"] = apply_filters($filter, '');
63 }
64
65 return apply_filters('opengraph_metadata', $metadata);
66 }
67
68
69 /**
70 * Register filters for default Open Graph metadata.
71 */
72 function opengraph_default_metadata() {
73 add_filter('opengraph_title', 'opengraph_default_title', 5);
74 add_filter('opengraph_type', 'opengraph_default_type', 5);
75 add_filter('opengraph_image', 'opengraph_default_image', 5);
76 add_filter('opengraph_url', 'opengraph_default_url', 5);
77
78 add_filter('opengraph_site_name', 'opengraph_default_sitename', 5);
79 add_filter('opengraph_description', 'opengraph_default_description', 5);
80 }
81 add_filter('wp', 'opengraph_default_metadata');
82
83
84 /**
85 * Default title property, using the page title.
86 */
87 function opengraph_default_title( $title ) {
88 if ( is_singular() && empty($title) ) {
89 global $post;
90 $title = $post->post_title;
91 }
92
93 return $title;
94 }
95
96
97 /**
98 * Default type property.
99 */
100 function opengraph_default_type( $type ) {
101 if ( empty($type) ) $type = 'blog';
102 return $type;
103 }
104
105
106 /**
107 * Default image property, using the post-thumbnail.
108 */
109 function opengraph_default_image( $image ) {
110 global $post;
111 if ( function_exists('has_post_thumbnail') ) {
112 if ( is_singular() && empty($image) && has_post_thumbnail($post->ID) ) {
113 $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-thumbnail');
114 if ($thumbnail) {
115 $image = $thumbnail[0];
116 }
117 }
118 }
119
120 return $image;
121 }
122
123
124 /**
125 * Default url property, using the permalink for the page.
126 */
127 function opengraph_default_url( $url ) {
128 if ( empty($url) ) $url = get_permalink();
129 return $url;
130 }
131
132
133 /**
134 * Default site_name property, using the bloginfo name.
135 */
136 function opengraph_default_sitename( $name ) {
137 if ( empty($name) ) $name = get_bloginfo('name');
138 return $name;
139 }
140
141
142 /**
143 * Default description property, using the bloginfo description.
144 */
145 function opengraph_default_description( $description ) {
146 if ( empty($description) ) $description = get_bloginfo('description');
147 return $description;
148 }
149
150
151 /**
152 * Output Open Graph <meta> tags in the page header.
153 */
154 function opengraph_meta_tags() {
155 global $opengraph_prefix_set;
156
157 $prefix = '';
158 if ( !$opengraph_prefix_set ) {
159 $prefix = 'prefix="og: ' . esc_attr(OPENGRAPH_PREFIX_URI) . '" ';
160 }
161
162 $metadata = opengraph_metadata();
163 foreach ( $metadata as $key => $value ) {
164 if ( empty($key) || empty($value) ) continue;
165 echo '<meta ' . $prefix . 'property="' . esc_attr($key) . '" content="' . esc_attr($value) . '" />' . "\n";
166 }
167 }
168 add_action('wp_head', 'opengraph_meta_tags');
169
170