How to turn plain URLs into clickable links in WordPress

The marvellous P2 Theme has an interesting feature: write out a plain link, and it magically becomes clickable without explicitly adding the a href tag.

This may not be a big deal if you’re writing posts in the visual WordPress editor rather than HTML, but for those of us who like to write in HTML, it’s just one less thing to worry about.

I was investigating this feature recently, and it turns out WordPress has a built-in function that can do this: they call it make_clickable(), and it works with URIs, FTP, Email addresses and anything starting with www. The function is really easy to use too: it only takes one parameter (a string), and returns the clickable HTML code.

Let’s see how to use it in context, using the TwentyThirteen theme.

Take a look at content.php, a template part that prints out full posts among many other things. Find a section with the class of entry-content, and you’ll see something like this:

This rather convoluted looking block is responsible for printing the_content. On this occasion it also replaces the standard “read more” text with something more palatable, and at the same time translates the whole thing too.

Let’s first simplify this by removing the translation and simply printing the content, including the handsome “Continue Reading” message:

Much easier on the eyes, and does exactly the same for the English version of TwentyThirteen.

Next, let’s do a bit of gentle tweaking: the_content() will print the current posts’s text to the browser. We can’t use this function though because we want to make some modifications before showing it to our audience. Instead we’ll use a related function: get_the_content().

It works *almost* like the_content(), but it will give us raw text without a small filtering applied, so we run the risk of introducing unwanted artefacts on our site. Thankfully it’s easy to apply said filtering manually to get_the_content:

And there we have it: first we store the output of get_the_content() in our variable $postContent. Then we apply the filtering so it will deliver the same goodness as the_content() does, and finally we make the $postContent clickable.

The WordPress Codex has more tips and tricks on both functions:

About Jay Versluis

Jay is a medical miracle known as a Super Survivor. He runs two YouTube channels, five websites and several podcast feeds. To see what else he's up to, and to support him on his mission to make the world a better place, check out his Patreon Campaign.

1 thought on “How to turn plain URLs into clickable links in WordPress

Add your voice!