XF 1.1 Changing link behaviour

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?
 

Jake Bunce

Well-known member
This is the relevant code:

library/XenForo/Helper/String.php

Removing the red code should do it:

Rich (BB 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.
 

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);
}
 

New Joe

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

rafass

Well-known member
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:
Top