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. $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.  
  6. foreach ( $textarr as $piece ) { 
  7.  
  8. 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 ) ) { 
  9. $nested_code_pre++; 
  10. } elseif ( $nested_code_pre && ( '</code>' === strtolower( $piece ) || '</pre>' === strtolower( $piece ) || '</script>' === strtolower( $piece ) || '</style>' === strtolower( $piece ) ) ) { 
  11. $nested_code_pre--; 
  12.  
  13. if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1, 20}+://|', $piece ) ) ) { 
  14. $r .= $piece; 
  15. continue; 
  16.  
  17. // Long strings might contain expensive edge cases ... 
  18. if ( 10000 < strlen( $piece ) ) { 
  19. // ... break it up 
  20. foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses 
  21. if ( 2101 < strlen( $chunk ) ) { 
  22. $r .= $chunk; // Too big, no whitespace: bail. 
  23. } else { 
  24. $r .= bbp_make_clickable( $chunk ); 
  25. } else { 
  26. $ret = " {$piece} "; // Pad with whitespace to simplify the regexes 
  27. $ret = apply_filters( 'bbp_make_clickable', $ret ); 
  28. $ret = substr( $ret, 1, -1 ); // Remove our whitespace padding. 
  29. $r .= $ret; 
  30.  
  31. // Cleanup of accidental links within links 
  32. return preg_replace( '#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r );