Build Open Graph
public function onBuildOpenGraph( JDocument $doc, VREViewContents $handler ) : void
Fires while including the OpenGraph directives within the head
of the document.
Description
The Open Graph protocol enables any web page to become a rich object in a social graph. For instance, this is used on Facebook to allow any web page to have the same functionality as any other object on Facebook.
This hook can be used to allow the plugins to include specific meta data to extend the Open Graph schema.
TIP: it is possible to add/alter meta data by using the following code:
$doc->setMetaData('og:title', 'This is my OG title');
Here's a list of meta data automatically generated by the plugin.
og:locale
- the default language of the website.og:locale:alternate
- an additional supported language. Repeats for each supported language.og:site_name
- the name of the website, taken from the WordPress configuration.
Parameters
- $doc
-
(JDocument) The document instance able to manipulate the head of the page.
- $handler
-
(VREViewContents) The handler instance used to manipulate the page contents.
Return Value
None.
Example
The example below explains how to include basic Open Graph for the details page of a product.
/**
* Trigger hook to allow the plugins to add meta data according to
* OPEN GRAPH protocol.
*
* @param JDocument $doc The document instance.
* @param VREViewContents $handler The page content handler.
*
* @return void
*/
public function onBuildOpenGraph($doc, $handler)
{
// get current plugin page
$page = $handler->page;
// make sure we are within the details page of an item
if (!preg_match("/^VikRestaurantsViewtakeawayitem$/i", get_class($page))) {
// do not go ahead.
return;
}
// get item object
$item = $page->item;
// extract and use short description
$description = $item->description;
$app = VREApplication::getInstance();
$app->onContentPrepare($description, $full = false);
// basic metadata
$doc->setMetaData('og:title' , $item->name);
$doc->setMetaData('og:description', $description->text);
$doc->setMetaData('og:type' , 'website');
// add product images if specified
foreach ($item->images as $image) {
$doc->setMetaData('og:image', VREMEDIA_URI . $image);
}
}
Changelog
Version | Description |
---|---|
1.8 | Introduced. |