XF 2.1 Custom canonical

We are working in integrating our site’s main backend and xenforo. One of the things we need is to customize the canonical url for threads (only threads in a specific forum) and some member pages (staff members), but we can’t figure out how to do this correctly:

The first thing we tried was writing an addon and extend the Thread Controller:

Code:
class Thread extends XFCP_Thread
{
   protected $myCanonical = null;

   public function actionIndex(\XF\Mvc\ParameterBag $params)
   {
        $res = parent::actionIndex($params);

        if ($somecondition) {
            $this->myCanonical = 'mycanonical.com';
        }

        return $res;
   }

   public function assertCanonicalUrl($linkUrl)
    {
        if(property_exists($this, 'myCanonical') && $this->myCanonical) {
            $linkUrl = $this->myCanonical;
        }
       
        return parent::assertCanonicalUrl($linkUrl);
    }
}

That code is trying to redirect to the new url, but we don't want to redirect just use that url as the canonical in meta for the thread.

Can you help us with some idea about what method we need to overwrite? or how is the best way to do this? Thank you!
 
Last edited:
we don't want to redirect just use that url as the canonical in meta for the thread.
That's handled in the thread_view template:

HTML:
<xf:macro template="metadata_macros" name="metadata"
    arg-description="{$fpSnippet}"
    arg-shareUrl="{{ link('canonical:threads', $thread) }}"
    arg-canonicalUrl="{{ link('canonical:threads', $thread, {'page': $page}) }}" />
 
Did you ever get the addon working?

I have done this successfully but only by creating a template modification with each thread I want with cross-site canonical which is a bit clunky.
 
Top Bottom