make_clickable

Convert plaintext URI to HTML links.

Description

(string) make_clickable( (string) $text ); 

Converts URI, www and ftp, and email addresses. Finishes by fixing links within links.

Returns (string)

Content with converted URIs.

Parameters (1)

0. $text (string)
Content to convert URIs.

Usage

  1. if ( !function_exists( 'make_clickable' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // Content to convert URIs. 
  5. $text = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = make_clickable($text); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function make_clickable( $text ) { 
  2. $r = ''; 
  3. $textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags 
  4. $nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code> 
  5. foreach ( $textarr as $piece ) { 
  6.  
  7. if ( preg_match( '|^<code[\s>]|i', $piece ) || preg_match( '|^<pre[\s>]|i', $piece ) || preg_match( '|^<script[\s>]|i', $piece ) || preg_match( '|^<style[\s>]|i', $piece ) ) 
  8. $nested_code_pre++; 
  9. elseif ( $nested_code_pre && ( '</code>' === strtolower( $piece ) || '</pre>' === strtolower( $piece ) || '</script>' === strtolower( $piece ) || '</style>' === strtolower( $piece ) ) ) 
  10. $nested_code_pre--; 
  11.  
  12. if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1, 20}+://|', $piece ) ) ) { 
  13. $r .= $piece; 
  14. continue; 
  15.  
  16. // Long strings might contain expensive edge cases ... 
  17. if ( 10000 < strlen( $piece ) ) { 
  18. // ... break it up 
  19. foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses 
  20. if ( 2101 < strlen( $chunk ) ) { 
  21. $r .= $chunk; // Too big, no whitespace: bail. 
  22. } else { 
  23. $r .= make_clickable( $chunk ); 
  24. } else { 
  25. $ret = " $piece "; // Pad with whitespace to simplify the regexes 
  26.  
  27. $url_clickable = '~ 
  28. ([\\s(<., ;:!?]) # 1: Leading whitespace, or punctuation 
  29. ( # 2: URL 
  30. [\\w]{1, 20}+:// # Scheme and hier-part prefix 
  31. (?=\S{1, 2000}\s) # Limit to URLs less than about 2000 characters long 
  32. [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+ # Non-punctuation URL character 
  33. (?: # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character 
  34. [\'., ;:!?)] # Punctuation URL character 
  35. [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character 
  36. )* 
  37. (\)?) # 3: Trailing closing parenthesis (for parethesis balancing post processing) 
  38. ~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character. 
  39. // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times. 
  40.  
  41. $ret = preg_replace_callback( $url_clickable, '_make_url_clickable_cb', $ret ); 
  42.  
  43. $ret = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=, ?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret ); 
  44. $ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2, })#i', '_make_email_clickable_cb', $ret ); 
  45.  
  46. $ret = substr( $ret, 1, -1 ); // Remove our whitespace padding. 
  47. $r .= $ret; 
  48.  
  49. // Cleanup of accidental links within links 
  50. return preg_replace( '#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r );