Skip to content

Instantly share code, notes, and snippets.

@MikSDigital
Created September 9, 2016 13:40
Show Gist options
  • Save MikSDigital/5287ef0021b773171d3c894b2187fddd to your computer and use it in GitHub Desktop.
Save MikSDigital/5287ef0021b773171d3c894b2187fddd to your computer and use it in GitHub Desktop.
set canonical URL in MODX
<?php
$context = $scriptProperties['context'] ?: 'ukltd';
//$context = (empty($scriptProperties['context'])) ? 'ukltd' : $scriptProperties['context'];
$linkedId = $modx->runSnippet('BabelTranslation', array(
'contextKey' => $context,
'resourceId' => $modx->resource->get('id')
));
$page = $modx->getObject('modResource', $linkedId);
$ctx = $modx->getContext($context);
$host = $ctx->getOption('http_host');
$linkedURL = 'http://' . $host .'/' . $page->get('uri') ;
$linkTag = "<link rel=\"canonical\" href=\"$linkedURL\"/>";
$modx->regClientStartupHTMLBlock($linkTag);
return '';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment