phpBB2Refugees.com Logo
Not affiliated with or endorsed by the phpBB Group

Register •  Login 

Continue the legacy...

Welcome to all phpBB2 Refugees!Wave Smilie

This site is intended to continue support for the legacy 2.x line of the phpBB2 bulletin board package. If you are a fan of phpBB2, please, by all means register, post, and help us out by offering your suggestions. We are primarily a community and support network. Our secondary goal is to provide a phpBB2 MOD Author and Styles area.

Recommend how to get topic_id in header? Canonical


 
Search this topic... | Search General Support... | Search Box
Register or Login to Post    Index » General Support  Previous TopicPrint TopicNext Topic
Author Message
bengurion
Board Member



Joined: 16 Sep 2018

Posts: 25



PostPosted: Sun Sep 16, 2018 1:56 pm 
Post subject: Recommend how to get topic_id in header? Canonical

I have bunch of links in head that are written there by {NAV_LINKS} tag.
So a pair of this links somehow easily shows a topic link in head with topic_id.

The question is - How to add new string in <head> with a topic url? I can only use topic_id in the body of the topic but not in <head>.
Something like this
<link rel="canonical" href="http://site.com/topic123456.html"/>
The question is - how to exactly get topic_id in header to write this string?

Thank you! And thanks god this forum was created. icon_biggrin.gif
Back to top
Salvatos
Board Member



Joined: 19 Feb 2009

Posts: 449
Location: Québec


flag
PostPosted: Sun Sep 16, 2018 9:27 pm 
Post subject: Re: Recommend how to get topic_id in header?

{TOPIC_ID} gets added to the template in viewtopic.php (which is run after the header is generated, as far as I know):
Code:
$topic_id = $post_id = 0;
if ( isset($HTTP_GET_VARS[POST_TOPIC_URL]) )
{
   $topic_id = intval($HTTP_GET_VARS[POST_TOPIC_URL]);
}
else if ( isset($HTTP_GET_VARS['topic']) )
{
   $topic_id = intval($HTTP_GET_VARS['topic']);
}

This is the initial check but there is something more being done with it further down the file, I'm not sure whether it would be useful in your case. Assuming this is enough, you would then need to copy-paste it somewhere into includes/page_header.php, before template vars are assigned around line 343 (and probably add an else case for when the header is loaded from somewhere other than viewtopic). Then you would simply add a line to the array like so (don't forget the comma):
Code:
   'NAV_LINKS' => $nav_links_html,
   'TOPIC_ID' => $topic_id)

And that template var would be available to you in any of your templates' overall_header.tpl.
Back to top
bengurion
Board Member



Joined: 16 Sep 2018

Posts: 25



PostPosted: Mon Sep 17, 2018 7:24 pm 
Post subject: Re: Recommend how to get topic_id in header?

Salvatos wrote:
{TOPIC_ID} gets added to the template in viewtopic.php (which is run after the header is generated, as far as I know):
Code:
$topic_id = $post_id = 0;
if ( isset($HTTP_GET_VARS[POST_TOPIC_URL]) )
{
   $topic_id = intval($HTTP_GET_VARS[POST_TOPIC_URL]);
}
else if ( isset($HTTP_GET_VARS['topic']) )
{
   $topic_id = intval($HTTP_GET_VARS['topic']);
}

This is the initial check but there is something more being done with it further down the file, I'm not sure whether it would be useful in your case. Assuming this is enough, you would then need to copy-paste it somewhere into includes/page_header.php, before template vars are assigned around line 343 (and probably add an else case for when the header is loaded from somewhere other than viewtopic). Then you would simply add a line to the array like so (don't forget the comma):
Code:
   'NAV_LINKS' => $nav_links_html,
   'TOPIC_ID' => $topic_id)

And that template var would be available to you in any of your templates' overall_header.tpl.


It works!!! I still can not believe this forum exist. It is the best thing that can be for the old school community builders. Image link
There is one more issue. If the topic is opened through "post" link, than topic_id returns 0(zero). How to get topic_id than? Image link
Back to top
Salvatos
Board Member



Joined: 19 Feb 2009

Posts: 449
Location: Québec


flag
PostPosted: Mon Sep 17, 2018 8:43 pm 
Post subject: Re: Recommend how to get topic_id in header?

bengurion wrote:
There is one more issue. If the topic is opened through "post" link, than topic_id returns 0(zero). How to get topic_id than? Image link

Something like the Last Post link from the topic list? That one would require a database lookup... Looking deeper into the code, I see there's a better way to proceed than what I suggested above.

page_header.php is actually called from viewtopic.php around line 575. That makes things a lot easier and less redundant for us because it can use variables that have already been set there. Basically I shouldn't have made you copy that initial part into page_header.php because it overwrote the version of $topic_id that is already generated, including the database check for cases such as direct linking to a post. So you can forget everything I said yesterday and just add TOPIC_ID to page_header.php and it should be fully functional:
Code:
   'NAV_LINKS' => $nav_links_html,
   'TOPIC_ID' => $topic_id)

Sorry for not realizing this from the start; I didn't expect it to be this convenient icon_biggrin.gif
Back to top
bengurion
Board Member



Joined: 16 Sep 2018

Posts: 25



PostPosted: Tue Sep 18, 2018 8:18 am 
Post subject: Re: Recommend how to get topic_id in header?

Salvatos wrote:
bengurion wrote:
There is one more issue. If the topic is opened through "post" link, than topic_id returns 0(zero). How to get topic_id than? Image link

Something like the Last Post link from the topic list? That one would require a database lookup... Looking deeper into the code, I see there's a better way to proceed than what I suggested above.

page_header.php is actually called from viewtopic.php around line 575. That makes things a lot easier and less redundant for us because it can use variables that have already been set there. Basically I shouldn't have made you copy that initial part into page_header.php because it overwrote the version of $topic_id that is already generated, including the database check for cases such as direct linking to a post. So you can forget everything I said yesterday and just add TOPIC_ID to page_header.php and it should be fully functional:
Code:
   'NAV_LINKS' => $nav_links_html,
   'TOPIC_ID' => $topic_id)

Sorry for not realizing this from the start; I didn't expect it to be this convenient icon_biggrin.gif



Yes you are right. There also a lot of other issues appeared at once icon_mrgreen.gif
Fixed it. And here is code maybe somebody will need it to make canonical links to phpbb2.
I have "rewrite" so the documents look like https://yourdomain.com/topic123.html. Or if there is other page it looks like https://yourdomain.com/topic123-10.html
Also i added pages numbers to code.

This goes to page_header.php
Code:
      
if ($start != 0)
   {
      $onpage = "-{$start}";
   }
   else
   {
      $onpage = '';
   }
   
   
   if ($topic_id != 0)
   {
      $canonical = "<link rel=\"canonical\" href=\"https://yourdomain.ru/topic{$topic_id}{$onpage}.html\"/>";
   }
   else
   {
      $canonical = '';
   }
   

And this in variables also in page_header.php

Code:
   'CANONICAL' => $canonical,
Back to top
Salvatos
Board Member



Joined: 19 Feb 2009

Posts: 449
Location: Québec


flag
PostPosted: Tue Sep 18, 2018 11:16 am 
Post subject: Re: Recommend how to get topic_id in header? Canonical

I'm glad you could sort it out! icon_smile.gif
Back to top
vlad77
Board Member



Joined: 31 May 2015

Posts: 122


flag
PostPosted: Wed Oct 31, 2018 9:05 am 
Post subject: Re: Recommend how to get topic_id in header? Canonical

I made a mod for <link rel="canonical" Rel_Canonical_URL.txt

"Basic" mod se_1.2.1.zip



se_1.2.1.zip
 Description:

Download
 Filename:  se_1.2.1.zip
 Filesize:  4.67 KB
 Downloaded:  1155 Time(s)


Rel_Canonical_URL.txt
 Description:

Download
 Filename:  Rel_Canonical_URL.txt
 Filesize:  3.65 KB
 Downloaded:  1292 Time(s)

Back to top
Display posts from previous:   
Register or Login to Post    Index » General Support  Previous TopicPrint TopicNext Topic
Page 1 of 1 All times are GMT - 4 Hours
 
Jump to:  

Index • About • FAQ • Rules • Privacy • Search •  Register •  Login 
Not affiliated with or endorsed by the phpBB Group
Powered by phpBB2 © phpBB Group
Generated in 0.1154 seconds using 19 queries. (SQL 0.0111 Parse 0.0148 Other 0.0895)
phpBB Customizations by the phpBBDoctor.com
Template Design by DeLFlo and MomentsOfLight.com Moments of Light Logo