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

XF 1.1 Changing link behaviour

Discussion in 'XenForo Questions and Support' started by Neil E., Aug 9, 2012.

  1. Neil E.

    Neil E. Active Member

    It looks like XenForo always opens external links (pages from other sites) in new windows/tabs which is fine. Internal links (within the forum) open in the same window/tab.

    If I wanted all links to open in new windows, are there settings to obtain this?
     
    Mario Gil likes this.
  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    This is the relevant code:

    library/XenForo/Helper/String.php

    Removing the red code should do it:

    Code:
    	/**
    	 * Gets the class and target to apply to a specified link URL.
    
    	 * @param string $url
    	 *
    	 * @return array [class, target, type (internal/external)]
    	 */
    	public static function getLinkClassTarget($url)
    	{
    		$target = '_blank';
    		$class = 'externalLink';
    		$type = 'external';
    
    		$urlInfo = @parse_url($url);
    		if ($urlInfo)
    		{
    			$host = $urlInfo['host'] . (!empty($urlInfo['port']) ? ":$urlInfo[port]" : '');
    			if ($host == XenForo_Application::$host)
    			{
    				$target = '';
    				$class = 'internalLink';
    				$type = 'internal';
    			}
    		}
    
    		return array($class, $target, $type);
    	}
    
    Or you can edit the bb code parser instead (which makes use of the helper):

    library/XenForo/BbCode/Formatter/Base.php

    See the renderTagUrl() function. You can extend this class through the addon system so this might be preferable to editing the helper.
     
    Mario Gil likes this.
  3. Neil E.

    Neil E. Active Member

    I assume your example would be a global change for all links.
    Is there a way to do this for an individual link?
     
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

  5. Neil E.

    Neil E. Active Member

    I don't have a specific need at the moment so I'll leave it for later on. Thanks.

    How would we manage without Jake's knowledge?
     
  6. 51463

    51463 Well-Known Member

    Since this thread was made in 2012. The coding has changed a bit.

    Can you tell me if i am correct to delete all of this? I tested it and it seems to work fine. Just wanted to double check i am not deleting anything good.

    This is what i deleted (in the red):

    /**
    * Gets the class and target to apply to a specified link URL.

    * @param string $url
    *
    * @return array [class, target, type (internal/external), scheme match]
    */
    public static function getLinkClassTarget($url)
    {
    $target = '_blank';
    $class = 'externalLink';
    $type = 'external';
    $schemeMatch = true;

    $urlInfo = @parse_url($url);
    if ($urlInfo)
    {
    if (empty($urlInfo['host']))
    {
    $isInternal = true;
    }
    else
    {
    $host = $urlInfo['host'] . (!empty($urlInfo['port']) ? ":$urlInfo[port]" : '');
    $isInternal = ($host == XenForo_Application::$host && strpos($url, 'proxy.php') === false);

    $scheme = (!empty($urlInfo['scheme']) ? strtolower($urlInfo['scheme']) : 'http');
    $schemeMatch = $scheme == (XenForo_Application::$secure ? 'https' : 'http');
    }

    if ($isInternal)
    {
    $target = '';
    $class = 'internalLink';
    $type = 'internal';
    }
    }


    return array($class, $target, $type, $schemeMatch);
    }
     
    rafass likes this.
  7. New Joe

    New Joe Active Member

    That's what I took out and worked fine
     
  8. rafass

    rafass Well-Known Member

    Perfect!!
    Thanks!
    Code:
            $urlInfo = @parse_url($url);
            if ($urlInfo)
            {
                if (empty($urlInfo['host']))
                {
                    $isInternal = true;
                }
                else
                {
                    $host = $urlInfo['host'] . (!empty($urlInfo['port']) ? ":$urlInfo[port]" : '');
                    $isInternal = ($host == XenForo_Application::$host && strpos($url, 'proxy.php') === false);
    
                    $scheme = (!empty($urlInfo['scheme']) ? strtolower($urlInfo['scheme']) : 'http');
                    $schemeMatch = $scheme == (XenForo_Application::$secure ? 'https' : 'http');
                }
    
                if ($isInternal)
                {
                    $target = '';
                    $class = 'internalLink';
                    $type = 'internal';
                }
            }
     
    Last edited: Jan 12, 2015
    51463 likes this.

Share This Page