English (United Kingdom)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Microdata  >  Before Add Microdata

do_action_ref_array( 'vikrestaurants_before_add_microdata', mixed $dummy, object &$json, bool $includeVREViewContents $handler )

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

$dummy

(mixed)  Internal environment argument. Always ignore it.

&$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.


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  mixed            $dummy     Internal environment argument (ignore it).
 * @param  object           &$json     The JSON object.
 * @param  boolean          &$include  True whether the JSON object should be included to the document.
 * @param  VREViewContents  $handler   The page content handler.
 */
add_action('vikrestaurants_before_add_microdata', function($dummy, &$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();
}, 10, 4);

Changelog

Version Description
1.0 Introduced.
Last Update: 2021-01-24
Helpful?