• 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

Neil E.

Active member
#1
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?
 

Jake Bunce

XenForo moderator
Staff member
#2
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.
 

Neil E.

Active member
#5
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?
 

51463

Well-known member
#6
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);
}
 

New Joe

Active member
#7
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);
}
That's what I took out and worked fine
 

rafass

Well-known member
#8
Since this thread was made in 2012. The coding has changed a bit.[...]
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: