Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-07-04 21:19:35

stephan
Plugin Author
From: Bochum, Germany
Registered: 2004-07-26
Posts: 196
Website

Set a get variable and keep it for all links

Here’s what I’d like to do:

I wish to include Google Adsense on my page and give my visitors a chance to disable them if they wish to do so. So I thought I could add a GET variable (noads) and use smd_if to check whether it is set. This works like a charm. However all the existing links to articles, categories and sections don’t automatically add this GET variable (of course, why would they?).

In a second scenario a site’s visitors could select one of two languages. Again a GET variable comes to mind.

Now my question is, is there any kind of plug-in or built-in functionality to keep a set GET variable for all the existing links in my site? An automatic append would be awesome (ideally with a check if there are already GET variables, as with a search, and just add this one in the correct way).

Any help is greatly appreciated (I assume I am probably not the first with above use cases and maybe my approach is absolutely wrong?)!


Yoko for Textpattern – A free blog themeMinimum Theme – If all you want to do is write.
Note: I am currently not actively using Textpattern, so I am not in the forums very often

Offline

#2 2009-07-04 22:17:17

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,732
Website

Re: Set a get variable and keep it for all links

You could try storing the get variable in a txp:variable, then testing for the variable using txp:if_variable. adi_gps does the storing bit for you (use in place of smd_if) automatically. What I don’t know is how persistent the txp:variable is / how long it lasts / whether it gets reset by other site visitors (would love to hear some clarification on this).

The other option would be to write the get value in a cookie, then check against the cookie. chs_cookie could help here.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2009-07-05 02:34:32

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

Re: Set a get variable and keep it for all links

jakob wrote:

The other option would be to write the get value in a cookie, then check against the cookie. chs_cookie could help here.

Which would be the better IMHO. First, it doesn’t cause dublicate URLs with same content, and it is less work, because there is no need to add the GET parameter into every URL. And it also works TXP’s hard-coded comment submission URLs which you really can’t change (in a easy way).

Example of setting the cookie:

<txp:chs_set_cookie cookie="adsense" value="disabled" />

…which you could wrap with smd_if conditionals (or other get conditionals, or put it an section page – whatsover) so that the cookie is set when the visitor clicks some link. For example with rah_if_gps

<txp:rah_if_gps att="ads" val="disable">
	<txp:chs_set_cookie cookie="adsense" value="disabled" />
</txp:rah_if_gps>

Now the cookie is set if ?ads=disable is visited.

Then we check if the cookie is set. Because the plugin doesn’t use evalElse, it doesn’t support <txp:else /> tags so we have to first set the value into variable.

<txp:variable name="adsense" value='<txp:chs_if_cookie cookie="adsense" value="disabled">disabled</txp:chs_if_cookie>' />

Then we could surround your adsense codes with:

<txp:if_variable name="adsense" value="disabled">
	<txp:else />
		<!--
			Adsense code here.
		-->
</txp:if_variable>

Last edited by Gocom (2009-07-05 02:41:37)

Offline

#4 2009-07-05 15:47:29

stephan
Plugin Author
From: Bochum, Germany
Registered: 2004-07-26
Posts: 196
Website

Re: Set a get variable and keep it for all links

Thanks, that is almost what I need. I am still struggling with activating the Adsense ads again. I didn’t find a way to unset/delete the cookie and tried this instead (which does work, but seems a little too complicated):

<txp:rah_if_gps att="noads" val="1">
	<txp:php>
	 setcookie("adsense","",time()-60*60*24);
	 </txp:php>
</txp:rah_if_gps>
<txp:rah_if_gps att="noads" val="0">
	<txp:chs_set_cookie cookie="adsense" value="disabled" />
</txp:rah_if_gps>

Before I used PHP the remove the cookie I tried this code:

<txp:rah_if_gps att="noads" val="1">
	<txp:chs_set_cookie cookie="adsense" value="disabled" />
</txp:rah_if_gps>
<txp:rah_if_gps att="noads" val="0">
	<txp:chs_set_cookie cookie="adsense" value="enabled" />
</txp:rah_if_gps>

In order to check without having to open my cookies all the time I added:

The cookie is 
<txp:chs_if_cookie cookie="adsense" value="enabled">enabled</txp:chs_if_cookie>
<txp:chs_if_cookie cookie="adsense" value="disabled">disabled</txp:chs_if_cookie>

But this never actually changed the setting from disabled once it was set.

It seems I cannot change the value of the cookie again once it is set if I use chs_cookie. Did I miss something? I feel like I am getting close, so thanks for all your help so far :)

Last edited by stephan (2009-07-05 16:39:46)


Yoko for Textpattern – A free blog themeMinimum Theme – If all you want to do is write.
Note: I am currently not actively using Textpattern, so I am not in the forums very often

Offline

Board footer

Powered by FluxBB