1. 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

Discussion in 'XenForo Development Discussions' started by LPH, Dec 17, 2014.

  1. LPH

    LPH Well-Known Member

    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 ) {
                
    XenForo_Template_Public::setStyleId$styleId );
            } else {
                
    XenForo_Template_Public::setStyleId);
            }

            
    $languageId $visitor['language_id'];

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

            
    /** 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();
     
  2. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    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:...}
    
     
  3. LPH

    LPH Well-Known Member

    I understand, but I meant through php so all of the people who own the bridge don't have to change their template.
     
  4. Brogan

    Brogan XenForo Moderator Staff Member

    Which link in which template are you trying to programmatically change?
     
  5. Mr. Goodie2Shoes

    Mr. Goodie2Shoes Well-Known Member

    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($hrefPHP_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...
     
  6. LPH

    LPH Well-Known Member

    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.

    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.
     

Share This Page