wppaste
WordPress

make_clickable( string $text ): string

Since
0.71
Source
wp-includes/formatting.php:3067
Converts plaintext URI to HTML links.

Description

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

Compatibility

WordPress
since 0.71
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$textstring
Content to convert URIs.

Return value

string
Content with converted URIs.

Performance profile

How much work a call to make_clickable() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
15–16

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 106.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • regexregular expression over the whole inputpreg_split()called directly

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost make_clickable() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always15–16preg_split()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10615–1619
8.510615–1619
8.410615–161921 fewer instructions than PHP 8.3
8.312718–1919
8.212718–1919
8.112718–1919
7.412718–1919

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

Used by · 1

Source code

function make_clickable( $text ) {	$r               = '';	$textarr         = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // Split out HTML tags.	$nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code>.	foreach ( $textarr as $piece ) { 		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 )		) {			++$nested_code_pre;		} elseif ( $nested_code_pre			&& ( '</code>' === strtolower( $piece )				|| '</pre>' === strtolower( $piece )				|| '</script>' === strtolower( $piece )				|| '</style>' === strtolower( $piece )			)		) {			--$nested_code_pre;		} 		if ( $nested_code_pre			|| empty( $piece )			|| ( '<' === $piece[0] && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) )		) {			$r .= $piece;			continue;		} 		// Long strings might contain expensive edge cases...		if ( 10000 < strlen( $piece ) ) {			// ...break it up.			foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing parentheses.				if ( 2101 < strlen( $chunk ) ) {					$r .= $chunk; // Too big, no whitespace: bail.				} else {					$r .= make_clickable( $chunk );				}			}		} else {			$ret = " $piece "; // Pad with whitespace to simplify the regexes. 			$url_clickable = '~				([\\s(<.,;:!?])                                # 1: Leading whitespace, or punctuation.				(                                              # 2: URL.					[\\w]{1,20}+://                                # Scheme and hier-part prefix.					(?=\S{1,2000}\s)                               # Limit to URLs less than about 2000 characters long.					[\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+         # Non-punctuation URL character.					(?:                                            # Unroll the Loop: Only allow punctuation URL character if followed by a non-punctuation URL character.						[\'.,;:!?)]                                    # Punctuation URL character.						[\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++         # Non-punctuation URL character.					)*				)				(\)?)                                          # 3: Trailing closing parenthesis (for parenthesis balancing post processing).				(\\.\\w{2,6})?                                 # 4: Allowing file extensions (e.g., .jpg, .png).			~xS';			/*			 * The regex is a non-anchored pattern and does not have a single fixed starting character.			 * Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times.			 */ 			$ret = preg_replace_callback( $url_clickable, '_make_url_clickable_cb', $ret ); 			$ret = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret );			$ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret ); 			$ret = substr( $ret, 1, -1 ); // Remove our whitespace padding.			$r  .= $ret;		}	} 	// Cleanup of accidental links within links.	return preg_replace( '#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', '$1$3</a>', $r );}

Changelog

Introduced in 0.71. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.