The Open Graph Pro WordPress Plugin offers public methods (functions) and filter hooks to provide access to the Open Graph Protocol metadata from themes and other plugins.
Methods
get_metadata()
Retrieve the Open Graph Protocol meta information for the current view, or (if in the loop) for the current post or page
Parameters
None
Return value
Array containing the Open Graph Protocol information
array ( "og" => array ( // "og" is the (XML) namespace prefix used for Open Graph Protocol metadata "id" => "http://ogp.me/ns#", // Contains the (XML) namespace identifier "metadata" => array ( // Array containing all the meta information for this namespace "title" => "", // Title of the page the tags are on "site_name" => "", // Name of the overall site "description" => "", // Description/abstract of the page the tags are on "type" => "article", // Object type of the Facebook page "url" => "", // URL of the Facebook page "image" => "", // Image to be shown in the news feed(s) )) "fb" => array ( // "fb" is the (XML) namespace prefix used for Facebook API tags – This section of the array is only used if at least one of the fields "admins" or "app_id" is not empty "id" => "http://www.facebook.com/2008/fbml", // Contains the (XML) namespace identifier "metadata" => array ( // Array containing all the meta information for this namespace "admins" => "", // Comma separated list of numerical Facebook user IDs who are eligible to become admins of this page "app_id" => "", // Numerical ID of an app which is eligible to stream updates to fans' news feeds )))
Other notes
The method is also used internally. Its results are passed through the filter “ogp_get_metadata” so you can hook into that filter to modify bits of the Open Graph Protocol meta information output on your pages
Example Usage
Short and easy way of adding a “Like” button (to make the <fb:like
tag work you must also call the Facebook JavaScript SDK somewhere on the page)
<?php if (class_exists('Open_Graph_Pro')) { $ogp_metadata = Open_Graph_Pro::get_metadata(); $url = esc_attr ( $ogp_metadata['og']['metadata']['url'] ); echo "<fb:like href=\"$url\" layout=\"button_count\"></fb:like>"; }
Filter hooks
ogp_get_metadata
Called on the results of get_metadata()
Example Usage
See below
ogp_types_by_category
Called on the array of object types which can be selected on the Open Graph Pro Settings screen and on the page editing screens
Example Usage
See below
Example Usage for both filter hooks
Facebook encourages the use of custom object types. According to their documentation these should be placed in your own namespace. The example from developers.facebook.com could be implemented (and expanded) like so:
<?php function moviesite_types_by_category($a) { $a['moviesite.com']=array('name'=>'moviesite.com','types'=>array( 'moviesite:actor'=>'Actor', 'moviesite:director'=>'Director', 'moviesite:dop'=>'D.O.P.', 'moviesite:makeup'=>'Make-up Artist', )); return $a; } function moviesite_get_metadata($a) { if ('moviesite'==substr($a['og']['metadata']['type'],0,9)) $a['moviesite']=array('id'=>'http://www.moviesite.com/ns#', 'metadata'=>array(),); return $a; } add_filter('ogp_types_by_category','moviesite_types_by_category'); add_filter('ogp_get_metadata','moviesite_get_metadata');
No comments
Comments feed for this article
Trackback link: https://ten-fingers-and-a-brain.com/wordpress-plugins/ogp/api/trackback/