Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
[SOLVED] Encode <txp:search_term /> for a URL
I’d like to do something to the output of search_term
to format it as part of a valid search URL. The reason? I want to populate the canonical
meta tag in head
. I’m a sucker, I know.
If I search for this:
foo bar
… the output of search_term
is, naturally:
foo bar
To make this a URL component, I need to switch the space with a +
, like this:
foo+bar
Easy enough. However, if I search for this:
“foo bar”
…that then introduces double quotes that need escaping/flattening, plus the space from before. If I percent-encode for the URL, I get this:
%22foo+bar%22
That’s the double quotes covered…but the list of characters that need to processed is pretty sizeable.
Working on the principle that people will do unexpected things on the Internet, I’m looking for a way to process the text from search_term
and then encode it for a URL. Short of a complex and unwieldy rah_replace
arrangement, I am unsure how to best achieve this.
One idea I had was to snag the search results URL and bodge it with site_url
, but I’m curious if I can process text in a cleaner fashion.
I’m on PHP 5.4 if that makes any difference.
Thank you in advance, and best wishes to you all.
Last edited by gaekwad (2013-11-29 17:15:16)
Offline
Re: [SOLVED] Encode <txp:search_term /> for a URL
Balls. Just ignore me. I figured it out: page_url
can snag the search term.
As you were.
Offline
Re: [SOLVED] Encode <txp:search_term /> for a URL
For completeness, this works to populate canonical
for most page states:
<txp:if_article_list>
<txp:if_section name="default">
<txp:if_search>
<link rel="canonical" href="<txp:site_url />?q=<txp:page_url type='q' />"/>
<txp:else />
<txp:if_category>
<link rel="canonical" href="<txp:site_url />category/<txp:page_url type='c' />"/>
<txp:else />
<txp:if_author>
<link rel="canonical" href="<txp:site_url />author/<txp:page_url type='author' />"/>
<txp:else />
<link rel="canonical" href="<txp:site_url />"/>
</txp:if_author>
</txp:if_category>
</txp:if_search>
<txp:else />
<link rel="canonical" href="<txp:site_url /><txp:section />" />
</txp:if_section>
<txp:else />
<txp:if_status status="200">
<link rel="canonical" href="<txp:permlink />" />
</txp:if_status>
</txp:if_article_list>
Offline