10

If I want to convert text into a URL friendly string for sharing text in a social media link, how would I go about that?

2 Answers 2

13

I was about to ask this question, but realized that it's closely related to this question.

The answer, like in the above referenced question lies with Twig filters.

Specifically, in this case, one needs to use the url_encode filter to convert the text needed.

Example

<a href="http://www.linkedin.com/shareArticle?mini=true&url={{ entry.url|url_encode }}&title={{ entry.title|url_encode }}&source=Xavier%20Creative%20House&summary={{ entry.question|url_encode|striptags }}" target="_blank">

In any case, I hope that helps others with this. I know how easy it is to forget that the answers sometimes like over in the Twig docs, and not Craft’s.

1

It was clearly documented on Twig Official Documentation that we can use url_encode filter to convert it to url friendly string.

Example

{{ "path-seg*ment"|url_encode }}
{# outputs "path-seg%2Ament" #}

{{ "string with spaces"|url_encode }}
{# outputs "string%20with%20spaces" #}

{{ {'param': 'value', 'foo': 'bar'}|url_encode }}
{# outputs "param=value&foo=bar" #}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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