Configure a custom sidebar link to open in a new tab

AI-generated summary

The discussion centers on whether Discourse admins should be able to configure specific custom sidebar links to open in a new tab (_blank), overriding the global user preference for how external links behave. kinetiksoft initially asks for this feature, arguing that external links (e.g., to a Telegram channel) should open in a new tab to preserve the user’s current experience on the forum. mcwumbly notes that Discourse currently respects user preferences, and kinetiksoft clarifies they are not asking for a global default change, but rather an admin-controlled option for specific links.

However, pfaffman raises a strong accessibility concern, noting that forcing new tabs inconveniences screen reader users who do not expect it. This point is supported by Lilly and ClawdiaWolf, who agree that respecting user preference is crucial for accessibility. pfaffman suggests admins can simply set the global user preference to open external links in new tabs if desired.

The conversation shifts when haydenjames asks how to disable new tabs for a specific link while keeping them enabled globally. Lilly explains the global settings, but Moin and haydenjames clarify they need per-link control. pfaffman suggests looking at custom header link plugins for inspiration, but awesomerobot explains that the sidebar is intentionally designed to respect the external_links_in_new_tab setting uniformly to maintain consistency, and there are no plugin outlets for individual link targets. pfaffman notes that modifying the sidebar would require clever JavaScript. Finally, markschmucker shares a custom JavaScript workaround that removes the target attribute for specific domains, allowing seamless navigation between the forum and the main site.

Is it possible to make custom section link go _blank in order to be opened in a new tab? There are many theme components providing this ability too

3 Likes

We did discuss at some point whether external links in the sidebar should open in a new tab, but decided to respect the user preference.

Can you share a case or two from your own site where you feel like it’d be better to override that user preference and force the link to open in a new tab?

2 Likes

Sure. Maybe our approach is just different. I truly believe if the link is external it is much polite to forward user’s browser to separate tab. That prevents them from interrupting the experience they have/had while being at Forum pages.

Our forum is highly active, not sure it’s okay for a user to be interrupted by leaving the website altogether by following the link. The link leads to our Telegram channel, if this is the case.

In the end, I do not propose making external links open _blank by default. I suggest you give Discourse admins an ability to control how any links open from this specific component/configuration feature

3 Likes

Temporary solution: If you right click on an item, you get the option to open it in a new tab.

Agreed, your progress would never get interrupted when links open in a new tab.

i tend to agree with this. like the above scenario with a telegram channel, clearly there are going to be cases where overriding the user preferences is ideal for admin created custom links. in theme components we target _blank often so i’m not sure why this option can’t be here as part of the custom side-bar config modal (unless it’s a case of real estate and there is no room there).

i actually have a admin customized side-bar links on my forum as well where i would be inclined to override the user setting for links.

3 Likes

You believe that if the user has expressed the preference to have links opened in the same window it’s more polite not to do what they have expressly indicated?

People who use screen readers are the ones most inconvenienced by opening things in new tabs when they don’t expect it. That’s why it’s generally recommended not to hijack links into a new tab (even though it’s what you and I prefer).

You can change the user default preference to open in a new tab, and also apply that setting to all users. That will allow your site to be more polite without violating the user preferences.

5 Likes

this is a really good point.

6 Likes

As someone who actually uses a screen reader (and all the fun issues some theme components create for them) I can confirm, it’s generally a bad idea to force new tabs.

6 Likes

How can I disable the sidebar from opening a custom link to a sister website, in a new tab? Since the other website also has a link to return to Discourse. At present, it results in a number of unwanted tabs.

Would prefer just for that external link to open to _self.

Thanks

1 Like

Generally all links must open to _self. An user says when and which ones should open to new tab or window.

1 Like

In admin - settings, make sure the following is disabled:

Note that logged in users can override the admin settings in their user preferences under the interface tab:

3 Likes

I think the question was how to set this up for one specific link to the site, which has a link back to the forum. Changing the admin setting would change the behavior for all links, wouldn’t it?

3 Likes

hmmm, could be. Do they want to override the user settings? Those are the only settings I know of that affect links in separate tabs/windows.

2 Likes

Yes, I don’t want to disable target _blank globally for external sites. Just want to disable it for this single menu item.

Thanks for explanation.

2 Likes

Custom Header Links lets you choose the target for the links it adds. Maybe this will help

https://github.com/discourse/discourse-custom-header-links/blob/main/javascripts/discourse/components/custom-header-links.hbs#L18

4 Likes

Thanks. So use this for the sidebar?

1 Like

It’s a hint. It’s the best I’ve got. TBH I’m not sure about modifying the sidebar.

It seems that there are still no plugin outlets in the sidebar (!?), so it might take some clever javascript to find the thing you want to modify. Something like this, maybe.

4 Likes

This is intentional, ideally the sidebar should be extended using the API… this helps us maintain any custom links/sections added here.

At the moment it looks like the external link targets are all or nothing based on the user setting external_links_in_new_tab… so even with a custom section/link added via the API there’s not a method to change the target for a specific link. It would be possible to add this option… though historically we tend to lean towards respecting the user’s preference for how links are opened.

5 Likes

That explains it. I guess I missed that memo. But looking a bit more closely, I see addSidebarPanel and addSidebarSection in plugin-api.js but only the latter turns up a post, and it’s far from a how-to. But there’s this:

https://github.com/discourse/discourse/blob/main/app/assets/javascripts/discourse/app/lib/plugin-api.js#L2306-2310

5 Likes

Correct. I have a link to parent website that I would prefer not to open in a new tab/window because visitors often go back and forth. Or rather, go forth and back. :joy: