How to turn URLs into clickable links in the_content()

- by

The P2 theme has a nice feature built-in: the ability to turn URLs into clickable links on the fly. It does this by using a WordPress built-in function called make_clickable().

Here’s how we can use this function to make this feature available to any theme.

function clickable_content ($content){
  $content = make_clickable($content);
  return $content;
}
add_filter ('the_content', 'clickable_content');

The above code, once inserted into your child theme’s functions.php file, will take the_content(), pass it to the make_clickable() function, and then return it before it’s printed on the screen.

The advantage of using it this way is that no content in the database is modified, and it’s easy to remove this feature when it’s not needed anymore. Feel free to add conditions depending on categories or other factors (you could check if the string “http” is present in the_content(), or only do this with .com endings, etc).

Learn more about this function here: https://codex.wordpress.org/Function_Reference/make_clickable



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.