• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Change Relative Links in Template via PHP

LPH

Well-known member
#1
I have a WordPress widget that pulls the online users as shown below. This works great and renders the template on the WordPress page or post. The issue is the relative links are being rendered rather than the full board url.

LPH would link to lph/members/lph.1 instead of lph/community/members/lph.1

Since this is outside of the file structure of XenForo - how can I get the full path instead of relative path?

PHP:
        $dependencies = new XenForo_Dependencies_Public();

        $dependencies->preLoadData();

        $sessionModel = XenForo_Model::create( 'XenForo_Model_Session' )->getModelFromCache('XenForo_Model_Session');

        $visitor = XenForo_Visitor::getInstance();

        $styleId = $visitor['style_id'];

        if ( $styleId > 0 ) {
            XenForo_Template_Public::setStyleId( $styleId );
        } else {
            XenForo_Template_Public::setStyleId( 1 );
        }

        $languageId = $visitor['language_id'];

        if ( $languageId > 0 ) {
            XenForo_Template_Abstract::setLanguageId( $languageId );
        } else {
            XenForo_Template_Abstract::setLanguageId( 1 );
        }

        /** Pull onlineUsers */
        $onlineUsers = $sessionModel->getSessionActivityQuickList( $visitor->toArray(), array(
            'cutOff' => array(
                '>',
                $sessionModel->getOnlineStatusTimeout()
            )
        ), ( $visitor['user_id'] ? $visitor->toArray() : null ) );

        $userLimit = '';
        $page = '';
        $userPerPage = '';
        $onlineTotals = '';

        $params = array(
            'onlineUsers'  => $onlineUsers,
            'visitor'      => $visitor,
            'userLimit'    => $userLimit,
            'page'         => $page,
            'usersPerPage' => $userPerPage,
            'onlineTotals' => $onlineTotals
        );

        $template = $dependencies->createTemplateObject( 'sidebar_online_users', $params );

        echo $template->render();
 

Mr. Goodie2Shoes

Well-known member
#2
Create your own online users block template copying the contents from 'sidebar_online_users' template, change all
Code:
{xen:link ...}
to
Code:
{xen:link canonical:...}
 

LPH

Well-known member
#3
Create your own online users block template copying the contents from 'sidebar_online_users' template, change all
Code:
{xen:link ...}
to
Code:
{xen:link canonical:...}
I understand, but I meant through php so all of the people who own the bridge don't have to change their template.
 

Mr. Goodie2Shoes

Well-known member
#5
Use DOM to manipulate the links, e.g:
PHP:
$dom = new Zend_Dom_Query();
$dom->setDocumentHtml($template->render());
$query = $dom->query('a');
/** @var DOMElement $a */
foreach ($query as $a)
{
if ($a->hasAttribute('href'))
{
$href = $a->getAttribute('href');
if (!@parse_url($href, PHP_URL_HOST))
{
$href = ltrim($href, '/');
$a->setAttribute('href', XenForo_Application::getOptions()->get('boardUrl') . '/' . $href);
}
}
}

echo $query->getDocument()->saveHTML();
P.S. This is just a rough example...
 

LPH

Well-known member
#6
Which link in which template are you trying to programmatically change?
Great question. The links in the sidebar_online_users need to have the full path instead of relative. So I can change my own but this will not help everyone who uses the bridge, thus, needing it to be done via PHP.

Use DOM to manipulate the links, e.g:
Thank you for the example, I'm going to have to wrap my head around that code and figure out how that would work in a WP widget so that all links are not influenced.