SPIP add GET variable to URL

286 Views Asked by At

I'm using SPIP with files of my own that are included according to keywords. For example, adding the keyword inc-bla to an article makes that the file .../squellettes/.../inc-bla.html is included. In one of those files, I want to make a link to the current page with a GET variable added to the URL:

[(#ENV{date}|diffdays|<{40}|?{<a href="#URL_ARTICLE?date=[(#ENV{date}|next_sunday)]">Some text...</a>,''})]

Now the problem is that the question mark ? is hard-coded, and sometimes the URL already has a question mark. So this code creates links like ...spip.php?article123?date=2014-03-30, which should be ...spip.php?article123&date=2014-03-30. I cannot hard-code an ampersand & because not every link has a question mark in it already.

Is there a way to add a GET variable to a URL in SPIP?

1

There are 1 best solutions below

1
On BEST ANSWER

The right way is using filters. We need to create our own filter, a PHP function like this:

function url_add_get_var($url,$name,$value) {
    $name = urlencode($name);
    $value = urlencode($value);
    // If there is no question mark in the URL ...
    if (strpos($url,'?') === false) {
        // Add the variable with a question mark
        return "$url?$name=$value";
    } else {
        // If there is a question mark, add the variable with an ampersand
        return "$url&$name=$value";
    }
}

Now, we can use it in SPIP like this:

[(text|filter{var1,var2, ...})]

So, in this case:

[(#URL_ARTICLE|url_add_get_var{date,[(#ENV{date}|next_sunday)]})]

And the full code:

[(#ENV{date}|diffdays|<{40}|?{<a href="[(#URL_ARTICLE|url_add_get_var{date,[(#ENV{date}|next_sunday)]})]">Some text...</a>,''})]