I need to output a slug that will include characters like < and |. I realise that these can't be literal, so I had hoped to be able to swap them out for the URL encoded equivalents.

For example, the slug l<t

Would become: l%3Ct

Right now, though, if I actually try and save the entry it strips out all of the percent symbols (%) rendering the encoding useless. Is there a way to turn off this behaviour? Or escape the symbols somehow?

  • 1
    Just curious as to your use case for this? If they are being used as URLs, you usually want them short and straightforward… Jun 1 at 19:20
  • I'm building a database, where the slug should be the product name. Some products have these kinds of characters in their names, which would ideally be represented in the URL. If the slug could accept URI encoded characters, then it'll all work nicely, as browsers will interpret the real characters correctly. (If that last part doesn't sound like I would work, I should mention that I don't serve anything direct from Craft, but via the Elements API, so it does indeed work.) Jun 1 at 19:48

Basically not possible with Craft, currently.

Slugs go through a normalizeSlug() helper that removes what it thinks is HTML and inner-word punctuation.

What I suspect is tripping you up is when it splits on "words" here: https://github.com/craftcms/cms/blob/develop/src/helpers/ElementHelper.php#L121

Where only ., | and - are allowed punctuation, stripping out everything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.