There are cases when you want external links posted in your forum to go through your page first before redirecting to the external URL. Most sites do this. Here are the reasons why you would want to do this:
1. To track the clicks to the external links.
2. To display an interstitial page to inform/warn user that they are navigating away from your site.
See how Yelp does #2.
Without further ado, here's how to do it.
There are 2 parts to do this:
Create the Redirect Page:
1. Create a new page in Sitebuilder.
2. Drag and drop a PHP module.
Title: Redirecting... (or whatever you want)
PHP Code: (Change 5000 milliseconds to whatever delay you want)
3. Save the page and fill out the page information.
Save the page name: Redirecting... (or whatever you want to show up in the tab name)
Save the page URL based on the page name: redirect (this is what you're going to use when editing the PHP file)
Template option: Redirect Template
Edit the PHP File:
1. Logon to cPanel (or equivalent).
2. Edit the following PHP file:
3. Change these:
with these: (change $redirect_path with the appropriate path of the redirect URL)
4. Clear system cache in AdminCP > Maintenance > Clear System Cache.
Screenshot:
NOTE: Since this is a PHP hack, this change will be overwritten during upgrade. So you have to re-apply this change whenever you upgrade.
1. To track the clicks to the external links.
2. To display an interstitial page to inform/warn user that they are navigating away from your site.
See how Yelp does #2.
Without further ado, here's how to do it.
There are 2 parts to do this:
- Creating the redirect page. This is only for #2 above. For #1, you're on your own. If you already have set it up, you can skip this)
- Editing the php file to change the external links to point to the redirect page.
Create the Redirect Page:
1. Create a new page in Sitebuilder.
2. Drag and drop a PHP module.
Title: Redirecting... (or whatever you want)
PHP Code: (Change 5000 milliseconds to whatever delay you want)
PHP Code:
$url = isset($_GET['url']) ? urldecode($_GET['url']) : '';
if ($url) {
echo '<p>You are leaving this site and will be automatically redirected to <a href="' . htmlspecialchars($url) . '" id="vbmodsthatrock-external-link" rel="nofollow" onclick="return redirect(this.href);">' . htmlspecialchars($url) . '</a> in a moment.</p>
<script>
function redirect(url) {
location.replace(url);
return false;
}
// redirect to the external URL with a 5-second delay
setTimeout(function() {
redirect(document.getElementById("vbmodsthatrock-external-link").href);
}, 5000); // 5000 milliseconds = 5 seconds, change accordingly
</script>';
}
else {
echo 'Sorry, there is no link to redirect to. You must have followed an invalid link.';
}
3. Save the page and fill out the page information.
Save the page name: Redirecting... (or whatever you want to show up in the tab name)
Save the page URL based on the page name: redirect (this is what you're going to use when editing the PHP file)
Template option: Redirect Template
Edit the PHP File:
1. Logon to cPanel (or equivalent).
2. Edit the following PHP file:
Code:
/includes/vb5/template/bbcode.php
PHP Code:
// standard URL hyperlink
return "<a href=\"$rightlink\" target=\"_blank\"" . ($is_external ? ' rel="nofollow"' : '') . ">$text</a>";
with these: (change $redirect_path with the appropriate path of the redirect URL)
PHP Code:
$rightlink_host = parse_url($rightlink, PHP_URL_HOST);
$forum_baseurl = vB::getDatastore()->getOption('frontendurl');
$forum_host = parse_url($forum_baseurl, PHP_URL_HOST);
// if same host, then use the link directly
if (vB5_String::stripos($rightlink_host, $forum_host) !== false) {
return '<a href="$rightlink" target="_blank"' . ($is_external ? ' rel="nofollow"' : '') . ">$text</a>";
}
else { // link to your redirect page and pass the encoded external link
// specify path of the redirect URL (exclude forum base url) including the querystring parameter name
// this is the path you used when you created the redirect page in Sitebuilder
$redirect_path = "/redirect?url=";
return '<a href="' . $forum_baseurl . $redirect_path . urlencode($rightlink) . '" target="_blank"' . ($is_external ? ' rel="nofollow"' : '') . ">$text</a>";
}
4. Clear system cache in AdminCP > Maintenance > Clear System Cache.
Screenshot:
NOTE: Since this is a PHP hack, this change will be overwritten during upgrade. So you have to re-apply this change whenever you upgrade.
Comment