wppaste
WordPress

wpautop( string $text, bool $br = true ): string

Since
0.71
Source
wp-includes/formatting.php:446
Replaces double line breaks with paragraph elements.

Description

A group of regex replaces used to identify text formatted with newlines and replace double line breaks with HTML paragraph tags. The remaining line breaks after conversion become <br /> tags, unless $br is set to '0' or 'false'.

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
The text which has to be formatted.
$brbooloptional
If set, this will convert all remaining line breaks after paragraphing. Line breaks within <script>, <style>, and <svg> tags are not affected. Default true.Default: true

Return value

string
Text which has been converted into correct paragraph tags.

Performance profile

How much work a call to wpautop() 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
7–165

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
5

5 places 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 · 9 distinct outcomes

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

WhenInstructionsCalls it makes
always7none
!$text && empty($pre_tags)92–126wp_replace_in_html_tags(), preg_split()
!$text && !empty($pre_tags)101–135wp_replace_in_html_tags(), preg_split(), array_keys(), array_values()
$text && empty($pre_tags)106–141explode(), array_pop(), wp_replace_in_html_tags(), preg_split()
!$text && empty($pre_tags)107–141wp_replace_in_html_tags(), preg_split(), preg_replace_callback()
$text && !empty($pre_tags)115–150explode(), array_pop(), wp_replace_in_html_tags(), preg_split(), array_keys(), array_values()
!$text && !empty($pre_tags)116–150wp_replace_in_html_tags(), preg_split(), preg_replace_callback(), array_keys(), array_values()
$text && empty($pre_tags)121–156explode(), array_pop(), wp_replace_in_html_tags(), preg_split(), preg_replace_callback()
$text && !empty($pre_tags)130–165explode(), array_pop(), wp_replace_in_html_tags(), preg_split(), preg_replace_callback(), array_keys(), array_values()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1907–16515
8.51907–16515
8.41907–16515131 fewer instructions than PHP 8.3
8.33219–28415
8.23219–28415
8.13219–284151 more instruction than PHP 7.4
7.43209–28315

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 · 5

Source code

function wpautop( $text, $br = true ) {	$pre_tags = array(); 	if ( '' === trim( $text ) ) {		return '';	} 	// Just to make things a little easier, pad the end.	$text = $text . "\n"; 	/*	 * Pre tags shouldn't be touched by autop.	 * Replace pre tags with placeholders and bring them back after autop.	 */	if ( str_contains( $text, '<pre' ) ) {		$text_parts = explode( '</pre>', $text );		$last_part  = array_pop( $text_parts );		$text       = '';		$i          = 0; 		foreach ( $text_parts as $text_part ) {			$start = strpos( $text_part, '<pre' ); 			// Malformed HTML?			if ( false === $start ) {				$text .= $text_part;				continue;			} 			$name              = "<pre wp-pre-tag-$i></pre>";			$pre_tags[ $name ] = substr( $text_part, $start ) . '</pre>'; 			$text .= substr( $text_part, 0, $start ) . $name;			++$i;		} 		$text .= $last_part;	}	// Change multiple <br>'s into two line breaks, which will turn into paragraphs.	$text = preg_replace( '|<br\s*/?>\s*<br\s*/?>|', "\n\n", $text ); 	$allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)'; 	// Add a double line break above block-level opening tags.	$text = preg_replace( '!(<' . $allblocks . '[\s/>])!', "\n\n$1", $text ); 	// Add a double line break below block-level closing tags.	$text = preg_replace( '!(</' . $allblocks . '>)!', "$1\n\n", $text ); 	// Add a double line break after hr tags, which are self closing.	$text = preg_replace( '!(<hr\s*?/?>)!', "$1\n\n", $text ); 	// Standardize newline characters to "\n".	$text = str_replace( array( "\r\n", "\r" ), "\n", $text ); 	// Find newlines in all elements and add placeholders.	$text = wp_replace_in_html_tags( $text, array( "\n" => ' <!-- wpnl --> ' ) ); 	// Collapse line breaks before and after <option> elements so they don't get autop'd.	if ( str_contains( $text, '<option' ) ) {		$text = preg_replace( '|\s*<option|', '<option', $text );		$text = preg_replace( '|</option>\s*|', '</option>', $text );	} 	/*	 * Collapse line breaks inside <object> elements, before <param> and <embed> elements	 * so they don't get autop'd.	 */	if ( str_contains( $text, '</object>' ) ) {		$text = preg_replace( '|(<object[^>]*>)\s*|', '$1', $text );		$text = preg_replace( '|\s*</object>|', '</object>', $text );		$text = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $text );	} 	/*	 * Collapse line breaks inside <audio> and <video> elements,	 * before and after <source> and <track> elements.	 */	if ( str_contains( $text, '<source' ) || str_contains( $text, '<track' ) ) {		$text = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $text );

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.9.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.