Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2025-02-05 03:19:46

Melonking
Member
Registered: 2022-05-10
Posts: 24
Website

Redirect to external articles

Hi hi,
I feel like the answer may be quite simple but I’ve not found a way to do it easily off the bat!

I use my TXP site as a blog, however not all of my writing is hosted on my blog! I would still like my blog to act as a directory for all my writing though so that people can follow it as a single RSS feed and I can offer it as a single link.

I would like to have some articles that appear just like regular articles in the list with keywords, publish dates, excerpts etc ~ however when you click on them they redirect to an external link in a new tab instead of an actual article. It would also be handy if there was some icon next to them to identify them as external articles.

e.g. imagine this is my blog section sorted by most recent:
  • Article A – internal article
  • Article B – external article
  • Article C – internal article
  • Article D – internal article
  • ect

I suppose I could include some sort of HTML redirect, or JavaScript based redirect in the body of the article; but that seems hacky; I’m wondering if there’s a better way to do it?

Last edited by Melonking (2025-02-05 15:50:05)


The world will tremble

Offline

#2 2025-02-05 07:14:43

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,135
Website GitHub Mastodon Twitter

Re: Redirect to external articles

You could use a custom field and a php redirect in the head.

ie. you could have a custom field named external which could house the external urls.

In the head of the page add

<txp:if_custom_field name="external" />
<txp:php>header("Refresh:0; url=<txp:custom_field name='external' />"); </txp:php>
</txp:if_custom_field >

regarding a new tab. Maybe somebody could shed some light.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2025-02-05 07:23:14

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,575
Website GitHub

Re: Redirect to external articles

I would do that using a custom field. I do exactly this on a site of mine.

Create a field called (for example) “external_link” and put the destination URL in the articles you want to direct elsewhere. Then in your template code that displays article lists:

<txp:if_custom_field name="external_link">
<a href="<txp:custom_field name="external_link" />"><txp:title /></a>
<txp:else />
<txp:permlink><txp:title /></txp:permlink>
</txp:if_custom_field>

Job done, I think. If you add a class to your titles, you can also hook into them with CSS to display the external link indicator.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#4 2025-02-05 07:40:32

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,296
Website

Re: Redirect to external articles

Same idea as Stef above – and add target="_blank" to your links:

<a href="<txp:custom_field name="external_link" />" target="_blank"><txp:title /></a>

Yiannis‘ idea would work too, but you can’t force the destination page to open in a new tab, afaik.


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

#5 2025-02-05 07:41:17

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,135
Website GitHub Mastodon Twitter

Re: Redirect to external articles

Further to what Bloke suggested, I would include the external link attribute in the url.

<txp:if_custom_field name="external_link">
<a rel="external" href="<txp:custom_field name="external_link" />"><txp:title /></a>
<txp:else />
<txp:permlink><txp:title /></txp:permlink>
</txp:if_custom_field>

Also, If you do not have control over the external sites, I would have

<a rel="external noopener" href="<txp:custom_field name="external_link" />"><txp:title /></a>

… and here’s the css I’m using on our site

a[rel~=external]:after {
content:"\00a0\21D7";
display:inline-block;
position:relative;
padding:0
}

Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#6 2025-02-05 07:42:45

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,575
Website GitHub

Re: Redirect to external articles

Ooh, good catch on the rel. No need for the class then. Neat.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#7 2025-02-05 14:20:36

etc
Developer
Registered: 2010-11-11
Posts: 5,317
Website GitHub

Re: Redirect to external articles

Possibly, things would be a little easier if we considered url_titles like, say, https://forum.textpattern.com/ as external and render them as is via <txp:permlink />? I see little use for them otherwise.

Offline

#8 2025-02-05 18:47:03

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,575
Website GitHub

Re: Redirect to external articles

etc wrote #339005:

Possibly, things would be a little easier if we considered url_titles like, say, https://forum.textpattern.com/ as external and render them as is via <txp:permlink />?

That would be an excellent idea. Can’t see a downside. Anyone else? I think even multi-site would work fine.

Might need a bit of JS wrangling to make sure it doesn’t get overwritten when you hit Save f you set the url_title prior to publishing. It it might already handle that kind of situation.

Edit: would we consider any URL beginning with a protocol as external or only those that didn’t match the site domain set in prefs?

Last edited by Bloke (2025-02-05 18:48:43)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#9 2025-02-05 19:56:58

etc
Developer
Registered: 2010-11-11
Posts: 5,317
Website GitHub

Re: Redirect to external articles

Bloke wrote #339006:

would we consider any URL beginning with a protocol as external or only those that didn’t match the site domain set in prefs?

I would say any, less work.

Offline

#10 2025-02-05 21:24:25

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,575
Website GitHub

Re: Redirect to external articles

etc wrote #339009:

I would say any, less work.

Sounds like a plan.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#11 2025-02-07 01:19:41

Melonking
Member
Registered: 2022-05-10
Posts: 24
Website

Re: Redirect to external articles

Thank you for this info! The custom field system is working great!

Last edited by Melonking (2025-02-07 16:47:01)


The world will tremble

Offline

#12 2025-02-07 02:50:48

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,296
Website

Re: Redirect to external articles

etc wrote #339005:

Possibly, things would be a little easier if we considered url_titles like, say, https://forum.textpattern.com/ as external and render them as is via <txp:permlink />? I see little use for them otherwise.

That sounds interesting. How would that work in practical usage – e.g with the markup given above – article list, external link with rel and/or target attributes, internal links just “plain“.


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

Board footer

Powered by FluxBB