Skip to content

Instantly share code, notes, and snippets.

@iamrobert
Last active December 9, 2025 19:21
Show Gist options
  • Select an option

  • Save iamrobert/4e793b96acd0e459c3a68a5de7e386d6 to your computer and use it in GitHub Desktop.

Select an option

Save iamrobert/4e793b96acd0e459c3a68a5de7e386d6 to your computer and use it in GitHub Desktop.
<?php
defined('_JEXEC') or die('Restricted access');
/*
* =======================================================================
+ GET META DATA
-----------------------------------------------------------------------
+ SETTINGS
+ SET IMAGE + GALLERY FIELDS **
-----------------------------------------------------------------------
OPEN GRAPH META TAGS
-----------------------------------------------------------------------
+ SET CUSTOM PAGE TITLE ($page_title)
+ SET META DESCRIPTION
+ BANNER AS H1 TITLE ($showH1Banner)
-----------------------------------------------------------------------
+ SEF ($sef)
=======================================================================
+ OPEN GRAPH META TAGS SOCIAL SHARES
https://blog.kissmetrics.com/open-graph-meta-tags/
https://moz.com/blog/meta-data-templates-123
//TO AVOID DOUBLE META OG:DESCRIPTION - set your flexi description plugin Social suggest to no.
Social > Suggest field to listings -> No
https://i.imgur.com/qYKzKul.png
======================================================================
*/
/* + SETTINGS
======================================================================*/
$document = JFactory::getDocument();
$app = JFactory::getApplication();
//TEMPLATE PATH
$tpath = JURI::base(true) . '/templates/' . $app->getTemplate() . '/';
$tmpl_path = JPATH_BASE . '/templates/' . $app->getTemplate() . '/';
//$app = JFactory::getApplication('site');
$template = $app->getTemplate(true);
/* + SET IMAGE + GALLERY FIELDS
======================================================================*/
$galleryFieldName = 'gallery';
$galleryfield_id = 67;
$imagefield = 32;
/* + COMPANY NAME + LOGO = Set in Joomla Site Template
======================================================================*/
//
$company_brand_name = '';
$sitename = $app->get('sitename');
if ($lang->getTag() == 'zh-TW') {
$company_brand_name = $template->params->get('company_brand_name_zh');
} else {
$company_brand_name = $template->params->get('company_brand_name_en');
}
if ($company_brand_name == '') {
$company_brand_name = $sitename;
}
//TEST COMPANY NAME
//echo $company_brand_name;
//SET META SITENAME
$document->setMetaData('og:site_name', "$company_brand_name", 'property');
/* + META FIELDS
======================================================================*/
// PUBLISHER LOGO (112px x 112px) ?
// https://developers.google.com/search/docs/appearance/structured-data/logo
$company_logo_image_name = '';
if ($template->params->get('logoFile')) {
$company_logo_image_name = JURI::base() . $template->params->get('logoFile');
}
//AUTHOR
$author = $this->item->author;
$datePublished = "";
//PUBLISHED DATE
if (!empty($this->item->created)) {
$datePublished = date('Y-m-d', strtotime($this->item->created));
$datePublished = '"datePublished": "' . $datePublished . '",';
}
$dateModified = "";
//MODIFIED DATE
if (!empty($this->item->modified)) {
$dateModified = date('Y-m-d', strtotime($this->item->modified));
$dateModified = '"dateModified": "' . $dateModified . '",';
}
/* + URLS
======================================================================*/
//SITE URL:
$siteURL = 'http' . (empty($_SERVER['HTTPS']) ? '' : 's') . '://' . $_SERVER['SERVER_NAME'];
/* + SET CUSTOM PAGE TITLE
======================================================================*/
//$getBrowserTitle = '';
//$getBrowserTitle = $this->params->get('flexi_browser_title');
$getBrowserTitle = $this->params->get('custom_ititle');
if ($getBrowserTitle != '') {
$page_title = $getBrowserTitle;
} else {
$page_title = $document->getTitle();
}
//GET CURRENT PAGE URL;
$current_page_url = JURI::current();
// GET OPENGRAPH TYPE:
//https://developers.facebook.com/docs/reference/opengraph/
$fb_og_type = $this->params->get('fb_og_type');
//SET URL
$document->setMetaData('og:url', "$current_page_url", 'property');
$document->setMetaData('og:type', "$fb_og_type", 'property');
$document->setMetaData('twitter:card', "summary_large_image");
//SET TITLE
$document->setMetaData('og:title', "$page_title", 'property');
$document->setMetaData('twitter:title', "$page_title");
$document->setTitle($page_title);
/* + SET DESCRIPTION ($intro_text)
======================================================================*/
//SET INTROTEXT LENGTH
$intro_text_cut_text = $this->params->get('intro_text_cut_text', '160');
$intro_text = JHtmlString::truncate($item->introtext, $intro_text_cut_text, true, false);
$intro_text = str_replace('...', '', $intro_text);
//G
//$meta_description = $document->getMetaData("description");
/* + SET META DESCRIPTION
======================================================================
Pulls From and Order:
1) Menu
2) Item
3) Intro text ($intro_text)
4) Site
5) Use Intro Text as Meta Description (see item.xml)
======================================================================*/
//1) META DESCRIPTION FUNCTION
function setMetaData($document, $description)
{
$document->setDescription($description);
$document->setMetaData('twitter:description', $description);
$document->setMetaData('og:description', $description, 'property');
$document->setMetaData('description', $description, 'itemprop');
return $description;
}
//2) SET META DESCRIPTION ORDERING
$candidates = [
$menu->getParams()->get('menu-meta_description'), // Meta description from menu
$item->metadesc, // Item meta description
($this->params->get('use_intro_meta_desc') == 1) ? $intro_text : null, // Intro text as meta (if enabled)
$app->getCfg('MetaDesc') // Global site meta description
];
//3) FIND FIRST NON-EMPTY CANDIDATE + USE AS META DESCRIPTION
$json_description = '';
foreach ($candidates as $candidate) {
if (!empty($candidate)) {
$json_description = setMetaData($document, $candidate);
break;
}
}
//echo $json_description;
/* + IMAGE FIELD
======================================================================*/
$imageURL = '';
$JSONimageURL = '';
if (isset($item->fieldvalues[$imagefield])) {
if (isset($this->item->fieldvalues[$this->item->fields['image']->id])) {
$imageURL = $siteURL . $item->fields['image']->{"display_large_src"};
$document->setMetaData('twitter:image', "$imageURL");
$document->setMetaData('og:image', "$imageURL", 'property');
$JSONimageURL =
'"image": ["' . $imageURL . '"],';
}
}
/* + CONVERT BANNER INTO JS
======================================================================*/
$showH1Banner = '';
$showH1Banner = $this->params->get('show_h1_banner');
//USED IN UCM slider.php
$GLOBALS['showH1Banner'] = $showH1Banner;
/* + SEF - use for date item-header-block
======================================================================*/
$sef = "";
if ($lang->getTag() !== 'zh-TW') {
$sef = "en";
}
<?php if ($show_schema == 1) : ?>
<?php
if (($microdata_itemtype == 'Article') || ($microdata_itemtype == 'NewsArticle') || ($microdata_itemtype == 'BlogPosting')) :
?>
<?php
if ($json_description != '' && $imageURL != '') {
$structured_data .= '<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "' . $microdata_itemtype . '",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "' . JURI::current() . '"
},
"headline": ' . json_encode($document->getTitle(), JSON_UNESCAPED_UNICODE) . ',
' . $JSONimageURL . '
' . $datePublished . '
' . $dateModified . '
"author": {
"@type": "Person",
"name": ' . json_encode($author, JSON_UNESCAPED_UNICODE) . '
},
"publisher": {
"@type": "Organization",
"name": ' . json_encode($company_brand_name, JSON_UNESCAPED_UNICODE) . ',
"logo": {
"@type": "ImageObject",
"url": "' . $company_logo_image_name . '"
}
},
"description": ' . json_encode($json_description, JSON_UNESCAPED_UNICODE) . '
}
</script>';
}
?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment