Created
October 28, 2016 13:18
-
-
Save MikSDigital/6e5862b40c9c3ab9ef09a7712071e477 to your computer and use it in GitHub Desktop.
get_canonical snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//error_reporting(E_ALL); | |
//ini_set('display_errors', 1); | |
if (!function_exists('self_canonical')) | |
{ | |
function self_canonical($currentContext) | |
{ | |
global $modx; | |
$ctx = $modx->getContext($currentContext); | |
$host = $ctx->getOption('http_host'); | |
$uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); | |
$path = ($uri == 'index/') ? '' : $uri; | |
$scheme = (count(explode('.', $host)) > 2) ? 'http://' : 'http://www.'; | |
$linkedURL = $scheme . $host . $path; | |
$linkTag = "<link rel=\"canonical\" href=\"$linkedURL\"/>"; | |
$modx->regClientStartupHTMLBlock($linkTag); | |
} | |
} | |
if(!function_exists('canonical')) | |
{ | |
function canonical($currentContext) | |
{ | |
//echo "Canonical " . $currentContext; | |
return; | |
} | |
} | |
if(!function_exists('canonicalSpecial')) | |
{ | |
function canonicalSpecial($currentContext) | |
{ | |
global $modx; | |
$noCanonicalTemplates = [21,40]; // id's of templates where not to set canonical from this snippet (special rules for canonical) :: ARTICLES, PARENT_ABOUT | |
$page = $modx->getObject('modResource', $modx->resource->get('id')); | |
$template = $page->get('template'); | |
if (in_array($template, $noCanonicalTemplates)) | |
{ | |
$uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); | |
$uri_segments = explode('/', $uri); | |
$last_uri_segment = end($uri_segments); | |
if (is_numeric($last_uri_segment)) { // check if URI is like | |
//self_canonical($currentContext); | |
return; | |
} | |
} | |
else { | |
self_canonical($currentContext); | |
} | |
} | |
} | |
$currentContext = $modx->context->get('key'); | |
switch ($currentContext) { | |
case "ukltd": | |
//canonicalSpecial($currentContext); | |
//break; | |
case "ukltd-ru": | |
case "bg": | |
case "cl": | |
case "nl": | |
case "hr": | |
case "cz": | |
case "de": | |
case "ee": | |
case "es": | |
case "fr": | |
case "hu": | |
case "lt": | |
case "lv": | |
case "pl": | |
case "ro": | |
case "sl": | |
self_canonical($currentContext); | |
break; | |
case "au": | |
case "lv-ru": | |
case "ee-ru": | |
case "md-ru": | |
canonical($currentContext); | |
break; | |
default: | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment