Specifying custom <title> tag format per page type?

Is it possible to customise the format for title tags on a page type basis? If not, does anyone know of a plugin, or a plugin that could be used as a basis to create this functionality please?

We can currently do it for email titles:

[%{site_name}] %{optional_pm}%{optional_cat}%{topic_title}

It would be great if we could do this for the view Topic and view Category pages too.

Anyone have any thoughts on this please?

It is not currently possible to modify the title format.

What’s the purpose of changing the title? If it is for the benefit of users browsing the site, then it should be possible to override the title using a theme component. If you want to change the title that Google sees, then you would need to use a plugin.

2 Likes

It’s mainly for the purpose of Google and other bots but it makes sense for readers too (I just need to add one word to make what the forum is about more explicit).

Do you know of any links to threads or guides that could help with making such a plugin please David?

If you just want to add a single word to the end, you could add it to the “Title” site setting. Although that will appear in more places than just the page title.

For crawlers, a plugin would need to override the page_title method

https://github.com/discourse/discourse/blob/master/lib/topic_view.rb#L154

And for regular browsers it would need to override the titleToken method

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/routes/topic.js.es6#L22

I don’t think there’s a step-by-step guide for these kind of overrides, so if you can manage with the site setting change that would be a lot easier.

4 Likes

Hi David, sorry for taking so long to get back to this!

I’ve just created a plugin to override the page-title method however the site name is still being added on to the end (any additions I make are simply inserted before the end) so it looks like it’s being added somewhere else.

Could it be:

  <% content_for(:title) { @title || "#{gsub_emoji_to_unicode(@topic_view.page_title)} - #{SiteSetting.title}" } %>

In views/topics/show.html.erb?

I created a plugin to override the whole show.html.erb file but nothing happened. (I’m also not able to test with JS turned off on my dev machine - I just get a blank page, is that normal? Edit: posted here)

I answered over in that topic - hopefully once you bypass Ember CLI things will work as expected. If not, let us know

2 Likes