Skip to content

Instantly share code, notes, and snippets.

@jloescher
Last active April 16, 2020 02:38
Show Gist options
  • Save jloescher/fa71a9a92042c54cc7ea to your computer and use it in GitHub Desktop.
Save jloescher/fa71a9a92042c54cc7ea to your computer and use it in GitHub Desktop.
Dynamically Create Schema.org code for head section of a Magento 1.9 website.
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magento.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magento.com for more information.
*
* @category design
* @package base_default
* @copyright Copyright (c) 2006-2014 X.commerce, Inc. (http://www.magento.com)
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/
?>
<meta http-equiv="Content-Type" content="<?php echo $this->getContentType() ?>" />
<title><?php echo $this->getTitle() ?></title>
<!-- Schema.org Begin -->
<?php if(Mage::helper('core/url')->getCurrentUrl() === $this->getBaseUrl()): ?>
<!-- Schema.org Homepage Page Begin -->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Organization",
"logo": "<?php echo $this->getBaseUrl() . 'media/images/logo_small.jpg'; ?>",
"description": "<?php echo str_replace('&amp;', 'and',htmlspecialchars($this->getDescription())) ?>",
"mainEntityOfPage": "<?php echo $this->helper('core/url')->getCurrentUrl();?>",
"url": "<?php echo $this->getBaseUrl(); ?>",
"sameAs": [
"https://www.facebook.com/",
"https://twitter.com/",
"https://plus.google.com/",
"https://www.youtube.com/user/",
"https://www.pinterest.com/"
],
"brand": {
"@type": "http://schema.org/Brand",
"name": "Brand Name"
},
"potentialAction": {
"@type": "SearchAction",
"target": "https://domain.com/query/path/?q={search_term_string}",
"query-input": "required name=search_term_string"
}
}
</script>
<!-- Schema.org Homepage Page End -->
<?php elseif(Mage::registry('current_category') && !Mage::registry('current_product')): ?>
<!-- Schema.org Category Page Begin -->
<?php
//Get current category from registry
$currentCategory = Mage::registry('current_category');
//Display the current Category Title
$categoryCategoryName = $currentCategory->getName();
?>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "SomeProducts",
"description": "<?php echo str_replace('"',"''",strip_tags(($currentCategory->getMetaDescription()))); ?>",
"name": "<?php echo $categoryCategoryName; ?>",
"logo": "<?php echo $this->getBaseUrl() . 'media/images/logo_small.jpg'; ?>",
"url": "<?php echo $this->helper('core/url')->getCurrentUrl();?>",
"sameAs": [
"https://www.facebook.com/",
"https://twitter.com/",
"https://plus.google.com/",
"https://www.youtube.com/user/",
"https://www.pinterest.com/"
],
"mainEntityOfPage": "<?php echo $this->getBaseUrl(); ?>",
"brand": {
"@type": "http://schema.org/Brand",
"name": "Brand Name"
},
"category": {
"@type": "http://schema.org/Thing",
"name": "<?php echo $categoryCategoryName; ?>"
}
}
</script>
<!-- Schema.org Category Page End -->
<?php elseif(Mage::registry('current_product')): ?>
<!-- Schema.org Product Page Begin -->
<?php
$product = Mage::registry('current_product');
$categoryIds = $product->getCategoryIds();
foreach($categoryIds as $categoryId) {
$category = Mage::getModel('catalog/category')->load($categoryId);
$productCategoryName = $category->getName();
}
?>
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Product",
"name": "<?php echo str_replace(array(',', '"'), array('-', 'inch'), $product->getName()); ?>",
"description": "<?php echo str_replace('"',"''",strip_tags(($product->getDescription()))); ?>",
"image": "<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(200,200); ?>",
"sku": "<?php echo $product->getSku(); ?>",
"itemCondition": "NewCondition",
"mainEntityOfPage": "<?php echo Mage::registry('product')->getProductUrl(); ?>",
"manufacturer": {
"@type": "Organization",
"name": "Manufacturer Name"
},
"logo": "<?php echo $this->getBaseUrl() . 'media/images/logo_small.jpg'; ?>",
"url": "<?php echo $this->getBaseUrl(); ?>",
"sameAs": [
"https://www.facebook.com/",
"https://twitter.com/",
"https://plus.google.com/",
"https://www.youtube.com/user/",
"https://www.pinterest.com/"
],
"@context": "http://schema.org",
"@type": "Product",
"brand": {
"@type": "http://schema.org/Brand",
"name": "Brand Name"
},
"category": {
"@type": "http://schema.org/Thing",
"name": "<?php echo $productCategoryName; ?>"
}
}
</script>
<!-- Schema.org Product Page End -->
<?php else: ?>
<!-- Schema.org Normal Page Begin -->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Organization",
"description": "<?php echo str_replace('&amp;', 'and', htmlspecialchars($this->getDescription())) ?>",
"logo": "<?php echo $this->getBaseUrl() . 'media/images/logo_small.jpg'; ?>",
"mainEntityOfPage": "<?php echo Mage::helper('core/url')->getCurrentUrl(); ?>",
"name": "Organization Name",
"alternateName": "Organization Name",
"url": "<?php echo $this->getBaseUrl(); ?>",
"sameAs": [
"https://www.facebook.com/",
"https://twitter.com/",
"https://plus.google.com/",
"https://www.youtube.com/user/",
"https://www.pinterest.com/"
],
"brand": {
"@type": "http://schema.org/Brand",
"name": "Brand Name",
"url": "<?php echo $this->getBaseUrl(); ?>"
}
}
</script>
<!-- Schema.org Normal Page End -->
<?php endif; ?>
<!-- Schema.org End -->
<!-- Facebook Opengraph Begin -->
<!-- Facebook Opengraph Product Page Begin -->
<?php if(Mage::registry('current_product')): ?>
<?php $product = Mage::registry('current_product'); ?>
<meta property="og:title" content="<?php echo str_replace('"',"'",$product->getName()); ?>" />
<meta property="og:type" content="product" />
<meta property="og:image" content="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(200,200);?>" />
<meta property="og:url" content="<?php echo Mage::registry('product')->getProductUrl(); ?>" />
<meta property="og:description" name="description" content="<?php echo str_replace('"',"''",strip_tags(($product->getMetaDescription()))); ?>" />
<!-- Facebook Opengraph Product Page End -->
<!-- Facebook Opengraph Product Category Begin -->
<?php elseif(Mage::registry('current_category')): ?>
<meta property="og:title" content="<?php echo str_replace('"',"'",$this->getTitle()) ?>" />
<meta property="og:type" content="product.group" />
<meta property="og:url" content="<?php echo $this->helper('core/url')->getCurrentUrl();?>" />
<meta property="og:description" name="description" content="<?php echo str_replace('"',"''",strip_tags($this->getDescription())) ?>" />
<!-- Facebook Opengraph Product Category End -->
<!-- Facebook Opengraph Normal Pages Begin -->
<?php else: ?>
<meta property="og:url" content="<?php echo Mage::helper('core/url')->getCurrentUrl(); ?>" />
<meta property="og:image" content="<?php echo $this->getSkinUrl(Mage::getStoreConfig('design/header/logo_src')); ?>" />
<meta name="description" content="<?php echo htmlspecialchars($this->getDescription()) ?>" />
<?php endif; ?>
<!-- Facebook Opengraph Normal Pages End -->
<meta property="og:site_name" content="<?php echo Mage::getStoreConfig('general/store_information/name'); ?>" />
<!-- Facebook Opengraph End -->
<?php if(htmlspecialchars($this->getKeywords()) != ""): ?>
<meta name="keywords" content="<?php echo htmlspecialchars($this->getKeywords()) ?>" />
<?php endif; ?>
<meta name="robots" content="<?php echo htmlspecialchars($this->getRobots()) ?>" />
<?php //<meta name="viewport" content="width=device-width, initial-scale=1">php ?>
<link rel="icon" href="<?php echo $this->getFaviconFile(); ?>" type="image/x-icon" />
<link rel="shortcut icon" href="<?php echo $this->getFaviconFile(); ?>" type="image/x-icon" />
<link rel='publisher' href='https://plus.google.com/' />
<!--[if lt IE 7]>
<script type="text/javascript">
//<![CDATA[
var BLANK_URL = '<?php echo $this->helper('core/js')->getJsUrl('blank.html') ?>';
var BLANK_IMG = '<?php echo $this->helper('core/js')->getJsUrl('spacer.gif') ?>';
//]]>
</script>
<![endif]-->
<?php echo $this->getCssJsHtml() ?>
<?php echo $this->getChildHtml() ?>
<?php echo $this->helper('core/js')->getTranslatorScript() ?>
<?php echo $this->getIncludes() ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment