bbp_make_clickable

Convert plaintext URI to HTML links.

Description

(string) bbp_make_clickable( (string) $text ); 

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

This custom version of WordPress's make_clickable() skips links inside of pre and code tags.

Returns (string)

Content with converted URIs.

Parameters (1)

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

Usage

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

Defined (1)

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

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