Tags with spaces

Hey guys,

I’m trying to use tags with spaces in my site (e.g. “gordon freeman”). What kirby or my used template (Baltrum) generates is an URL like: /tag:gordon+freeman. Like this, of course, kirby doesn’t find any post, only if it has two tags “gordon” and “freeman”.

Any suggestions to this?

Thanks in advance!
Matti

Use urlencode() and urldecode(), e.g.

urldecode(param('tag'))

Thanks for your quick answer!

urlencode() and urldecode() is already used. See https://github.com/getkirby/plugins/blob/master/tagcloud/tagcloud.php.

Any further suggestions?

Try rawurlencode() and rawurldecode(), then …

Thanks again! But this also didn’t work.

Now the system gives me a link like site.tld/tag:gordon freeman
And on the result page it says “Posts tagged with ”gordon%20freeman“”, but still no results.

I guess it must be the built in URL interpreter of kirby.

Any further suggestions?

Could you post your code? I have been using tags with spaces without any problems in the past using urlencode() and urldecode() and with the tagcloud plugin that should work automatically anyway.

Thanks for the info that tags with spaces usually work in kirby!

That brought me to looking into the template files of my applied theme. There the author forgot to use urldecode(). I just added it and now it works fine:

/* Filter by tag, if needed */
if(param('tag', false)) {
    $posts = $posts->filterBy('tags', urldecode(param('tag')), ',');
}

Thanks again for your help!

1 Like

I am running into a similar issue. I have implemented urldecode and urlencode.

For example, for a category called Alpha Beta the encoded url is:

http://localhost/site/category:Alpha+Beta

When I click on that link, the browser loads the address

http://localhost/site/category:Alpha%20Beta

And at this point I get a generic browser window error saying:

This page isn’t working
localhost redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS

My controller code is:

return function($site, $pages, $page) {

  $collection = page('collection')->children()->visible();

  if ($tag = urldecode(param('category'))) {
    $collection = $collection->filterBy('tag', $tag, ',');
  }

  return compact('collection', 'tag');

};

The code that generate the link in my nav is:

<div class="nav">
  <ul>
    <?php foreach($tags as $tag): ?>
      <li>
        <a class="nav__link" href="<?= url('category:' . urlencode($tag['name']))?>">
          <?php echo $tag['name'] ?>
        </a>
      </li>
    <?php endforeach ?>
  </ul>
</div>

Have you tried with rawurlencode() and rawurldecode() instead?

I didn’t try rawurlencode().
I have instead create a new clone of a fresh kirby starterkit and tested that categories with spaces do work correctly.
After I rewrote the new installation using my old code. So far it’s working. It must have been some other error that was triggering ERR_TOO_MANY_REDIRECTS.

I have also the same problem with Umlauts (ü,ä,ö) but i cant’t find a way for that. I tried both rawurldecode() and urldecode()…do you have any ideas?

URL example: tld.com/farbe:Türkis

<li>
  <a <?php if(rawurldecode(param('farbe')) === $farbe ) { echo('class="aktiv"'); } ?> href="<?= url($page->url(), ['params' => array_merge(params(),['farbe' => rawurlencode($farbe)])]) ?>">
  <?= html($farbe) ?>
 </a>
<?php if( param('farbe') === $farbe ) { echo('<a class="closefilter" href="');echo($filteredURL);echo('">×</a>'); } ?>
</li>
<li>
  <a <?php if(urldecode(param('farbe')) === $farbe ) { echo('class="aktiv"'); } ?> href="<?= url($page->url(), ['params' => ['farbe' => urlencode($farbe)]]) ?>">
  <?= html($farbe) ?>
 </a>
</li>