Before Add Microdata
public function onBeforeAddMicrodata( object &$json, bool $include, VREViewContents $handler ) : void
Fires while including the microdata within the head
of the document.
Description
Microdata is used to nest metadata within existing content on web pages. Search engines and web crawlers can extract and process microdata from a web page and use it to provide a richer browsing experience for users. Google and other major search engines support the Schema.org vocabulary for structured data.
This hook can be used to allow the plugins to manipulate the JSON object that will be used by search engines.
Parameters
- &$json
-
(object) The JSON object including the microdata. An empty object by default.
- &$include
-
(bool) Whether to include the microdata or not (
false
by default). - $handler
-
(VREViewContents) The handler instance used to manipulate the page contents.
Return Value
None.
Example
The example below explains how to include basic microdata for the details page of a product.
/**
* Trigger event to allow the plugins to manipulate the JSON object
* that will be used by search engines.
*
* @param object &$json The JSON object.
* @param bool &$include True whether the JSON object should be included to the document.
* @param VREViewContents $handler The page content handler.
*
* @return void
*/
public function onBeforeAddMicrodata(&$json, &$include, $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;
}
// turn flag ON to include microdata
$include = true;
// get item object
$item = $page->item;
/**
* Define Product schema type.
*
* @link https://schema.org/Product
*/
$json->{"@context"} = 'http://schema.org';
$json->{"@type"} = 'Product';
// add product information
$json->name = $item->name;
$json->description = $item->description;
$json->url = (string) JUri::getInstance();
$json->category = $item->menu->title;
// extract and use short description
$app = VREApplication::getInstance();
$app->onContentPrepare($json->description, $full = false);
$json->description = $json->description->text;
// add product image, if specified
if ($item->image) {
$json->image = VREMEDIA_URI . $item->image;
}
/**
* Include Offer schema type.
*
* @link https://schema.org/Offer
*/
$json->offers = new stdClass;
$json->offers->{"@type"} = 'Offer';
$json->offers->price = $item->price;
$json->offers->priceCurrency = VREFactory::getCurrency()->getSymbol();
}
Changelog
Version | Description |
---|---|
1.8 | Introduced. |