English (United Kingdom)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  Microdata  >  Build Open Graph

do_action( 'vikrestaurants_build_open_graph', mixed $dummy, JDocument $doc, VREViewContents $handler )

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

$dummy

(mixed)  Internal environment argument. Always ignore it.

$doc

(JDocument)  The document instance able to manipulate the head of the page.

$handler

(VREViewContents)  The handler instance used to manipulate the page contents.


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  mixed            $dummy    Internal environment argument (ignore it).
 * @param  JDocument        $doc      The document instance.
 * @param  VREViewContents  $handler  The page content handler.
 */
add_action('vikrestaurants_build_open_graph', function($dummy, $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);
    }
}, 10, 3);

Changelog

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