MyBB Community Forums

Full Version: Open Forum Links In New Window?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,

Does anyone know if there is a way to make forum links open in a new window? I have set up a forum on my site, just for external links/resources, to help my members out. When they click one of these links, it opens the other site in the same window. Obviously, I don't want to direct people completely away from my site -- I would like my site to remain open in its original window. Is there a configuration option for this, that I am missing? This feature is very clunky to use, but I would really like to use it.

Thanks!
As far as I know, there is a way which is custom way & it can be done like this:


If you want your link to open a page in a new window use the target="_blank" in the <a href> tag.

Targetting the link to "_blank" simply opens a new browser window that will load the linked page.
<a href="new.html" target="_blank">Open new file</a>

Other then that, I don't think its possible Smile

I am looking for a way to find it and as soon as I do, I will let you know.
Open ./inc/class_parser.php file and find;
$link = "<a href=\"$fullurl\" target=\"_blank\">$name</a>";

and replace it to the following;
		if(strstr($fullurl,!$mybb->settings['bburl']))
            {
                $target = "_self";
            } else {
                $target = "_blank";
            }
		$link = "<a href=\"$fullurl\" target=\"$target\">$name</a>";
(2011-05-23, 07:36 AM)Yaldaram Wrote: [ -> ]
if(strstr($fullurl,!$mybb->settings['bburl']))

This will not work. Why casting a string value to a boolean? Undecided Makes no sense at all.
Also strstr() is not the appropriate function to use to check if a string contains another string. You should use strpos() instead.
if(strpos($fullurl,$mybb->settings['bburl']) !== false)
Hmmm... I changed this:
$link = "<a href=\"$fullurl\" target=\"_blank\">$name</a>";

To this:
if(strpos($fullurl,$mybb->settings['bburl']) !== false)
{
$target = "_self";
} else {
$target = "_blank";
}
$link = "<a href=\"$fullurl\" target=\"$target\">$name</a>";

Now, when I click one of the forum links, nothing ever opens. Did I do something wrong?

Thanks!
I hadn't checked out the complete code, but find this:
	function mycode_parse_url($url, $name="")
	{
and add this on a new line below it:
		global $mybb;

That should work.

However (!!!) there is a much better solution that doesn't require a core modification and that works with all URLs. Add a new MyCode via AdminCP > Configuration > MyCode with these settings:
Title: Open internal links in same window
Regular expression:
\<a\ href=\"(http://www.yourdomain.com/.*?)\" target=\"_blank\"
Replacement:
<a href="$1"
(2011-05-23, 01:43 PM)Aries-Belgium Wrote: [ -> ]I hadn't checked out the complete code, but find this:
	function mycode_parse_url($url, $name="")
	{
and add this on a new line below it:
		global $mybb;

That should work.

However (!!!) there is a much better solution that doesn't require a core modification and that works with all URLs. Add a new MyCode via AdminCP > Configuration > MyCode with these settings:
Title: Open internal links in same window
Regular expression:
\<a\ href=\"(http://www.yourdomain.com/.*?)\" target=\"_blank\"
Replacement:
<a href="$1"

Hmm, after adding that new line, now I get this error when loading the page:
Parse error: syntax error, unexpected T_GLOBAL, expecting ';' or '{' in /home2/greatlc9/public_html/testforum/inc/class_parser.php on line 1632

I'm not sure why it's complaining about line 1632 now, as that line is blank:
1631 $code = str_replace("<code>", "<div dir=\"ltr\"><code>", $code);
1632
1633 $code = str_replace("</code>", "</code></div>", $code);

As for your other option, that would be good for general use, but I don't think it will help me make the "Forum Links" open in new windows. You know how you can create a forum as just a link? That is what I want to open in a new window... The links that people post in threads are already opening in new windows, for the most part.

Thanks for your help!
Oh, I'm sorry I didn't read the complete thread. I based my answer on the core modification Yaldaram posted which doesn't do what you want. If you haven't made any other modification to that file, download a fresh MyBB zip from this site and replace the modified file with the one from the zip file.

For your initial request: I'm not a big fan of core modifications because it will cause problems when updating your MyBB installation. A template modification is less intrusive and easier to control when you upgrade your MyBB installation.
Install and activate this plugin: http://mybbhacks.zingaburga.com/showthread.php?tid=260
And then go to AdminCP > Templates & Style > Templates > Search/Replace.
Search for:
<a href="{$forum_url}">
Replace with:
<a href="{$forum_url}" <if $forum['linkto'] != '' then>target="_blank"</if>>
Then press Search/Replace.

That should work.
(2011-05-23, 02:21 PM)Aries-Belgium Wrote: [ -> ]Oh, I'm sorry I didn't read the complete thread. I based my answer on the core modification Yaldaram posted which doesn't do what you want. If you haven't made any other modification to that file, download a fresh MyBB zip from this site and replace the modified file with the one from the zip file.

For your initial request: I'm not a big fan of core modifications because it will cause problems when updating your MyBB installation. A template modification is less intrusive and easier to control when you upgrade your MyBB installation.
Install and activate this plugin: http://mybbhacks.zingaburga.com/showthread.php?tid=260
And then go to AdminCP > Templates & Style > Templates > Search/Replace.
Search for:
<a href="{$forum_url}">
Replace with:
<a href="{$forum_url}" <if $forum['linkto'] != '' then>target="_blank"</if>>
Then press Search/Replace.

That should work.

Sorry, I am trying to accomplish the same thing. The link for the plugin, the file is only 1.65KB. This going to sound stupid, but how am I suppose to install this plug in? Copy it to the root?

Any help would be greatly appreciated,
Tim
(2011-05-27, 04:59 AM)Tim_bc Wrote: [ -> ]Sorry, I am trying to accomplish the same thing. The link for the plugin, the file is only 1.65KB. This going to sound stupid, but how am I suppose to install this plug in? Copy it to the root?

No, download the .7z file from the page I linked to. You need 7zip to extract it. In that compressed file there is a file called phptl.php. Starting from the MyBB root, navigate to inc -> plugins and place that file in this folder. Now go to AdminCp > Configurtion > Plugins and search for "PHP in Templates" and click "Activate" at the end of the row.
Pages: 1 2