wpautop( string $text, bool $br = true ): string
- Since
- 0.71
- Source
wp-includes/formatting.php:446
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
- Scaling
- Scales with input
- Instructions
- 7–165
- Plugin surface
- None
- Called by
- 5
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 190.
Nothing here hands control to plugin code.
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 input
preg_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7 | none |
!$text && empty($pre_tags) | 92–126 | wp_replace_in_html_tags(), preg_split() |
!$text && !empty($pre_tags) | 101–135 | wp_replace_in_html_tags(), preg_split(), array_keys(), array_values() |
$text && empty($pre_tags) | 106–141 | explode(), array_pop(), wp_replace_in_html_tags(), preg_split() |
!$text && empty($pre_tags) | 107–141 | wp_replace_in_html_tags(), preg_split(), preg_replace_callback() |
$text && !empty($pre_tags) | 115–150 | explode(), array_pop(), wp_replace_in_html_tags(), preg_split(), array_keys(), array_values() |
!$text && !empty($pre_tags) | 116–150 | wp_replace_in_html_tags(), preg_split(), preg_replace_callback(), array_keys(), array_values() |
$text && empty($pre_tags) | 121–156 | explode(), array_pop(), wp_replace_in_html_tags(), preg_split(), preg_replace_callback() |
$text && !empty($pre_tags) | 130–165 | explode(), array_pop(), wp_replace_in_html_tags(), preg_split(), preg_replace_callback(), array_keys(), array_values() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 190 | 7–165 | 15 | |
| 8.5 | 190 | 7–165 | 15 | |
| 8.4 | 190 | 7–165 | 15 | 131 fewer instructions than PHP 8.3 |
| 8.3 | 321 | 9–284 | 15 | |
| 8.2 | 321 | 9–284 | 15 | |
| 8.1 | 321 | 9–284 | 15 | 1 more instruction than PHP 7.4 |
| 7.4 | 320 | 9–283 | 15 |
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
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_replace_in_html_tags()Replaces characters or phrases within HTML elements only.
Used by · 5
- Twenty_Twenty_One_Dark_Mode::add_privacy_policy_content()Adds information to the privacy policy.
- WP_Widget_Text::widget()Outputs the content for the current Text widget instance.
- render_block_core_latest_comments()Renders the `core/latest-comments` block on server.
- render_block_core_shortcode()Performs wpautop() on the shortcode block content.
- wp_richedit_pre()Formats text for the rich text editor.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.