Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2013-04-24 13:17:05

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,356
GitHub

Encoding/trimming output of <txp:site_url />

Hi.
A short question that hopefully someone can shed some light on. Is it possible to percent-encode the output of a tag (in this case site_url)? If so, how might I achieve this?

In this instance, I’m using a template holding page with a hyperlink to another site. This hyperlink includes Google Analytics tags to determine the referrer. To avoid potential mishaps down the line, I want to avoid using clunky URLs like this:

http://example.com?utm_source=http://example.org&utm_medium=fruit&utm_campaign=banana

Instead, I’d like to either percent-encode the source URL…:

http://example.com?utm_source=http%3A%2F%2Fexample.org&utm_medium=fruit&utm_campaign=banana

…or, perhaps, strip the http:// from the referrer:

http://example.com?utm_source=example.org&utm_medium=fruit&utm_campaign=banana

I can use php to chew the first part of the site_url off, but as it’s technically deprecated I’d prefer a more reliable method for now and the future.

Thank you very much in advance for any suggestions, I’m grateful for your time, attention and expertise.

Offline

#2 2013-04-24 17:39:12

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Encoding/trimming output of <txp:site_url />

You could either use PHP, which is not ‘technically’ deprecated, or a plugin. For instance rah_function happily does encoding tasks:

<txp:rah_function call="urlencode" url='<txp:site_url />' />

Or you could use a PHP block:

<txp:php>
    echo urlencode(parse('<txp:site_url />'));
</txp:php>

Offline

#3 2013-04-25 10:25:44

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,356
GitHub

Re: Encoding/trimming output of <txp:site_url />

Thank you, Jukka – that’s perfect.

Offline

Board footer

Powered by FluxBB