wpautop

Replaces double line-breaks with paragraph elements.

Description

(string) wpautop( (string) $pee, (bool) $br = true ); 

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 <
> tags, unless $br is set to 0 or false..

Returns (string)

Text which has been converted into correct paragraph tags.

Parameters (2)

0. $pee (string)
The text which has to be formatted.
1. $br — Optional. (bool) => true
If set, this will convert all remaining line-breaks after paragraphing. Default true.

Usage

  1. if ( !function_exists( 'wpautop' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The text which has to be formatted. 
  5. $pee = ''; 
  6.  
  7. // Optional. If set, this will convert all remaining line-breaks 
  8. // after paragraphing. Default true. 
  9. $br = true; 
  10.  
  11. // NOTICE! Understand what this does before running. 
  12. $result = wpautop($pee, $br); 
  13.  

Defined (1)

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

/wp-includes/formatting.php  
  1. function wpautop( $pee, $br = true ) { 
  2. $pre_tags = array(); 
  3.  
  4. if ( trim($pee) === '' ) 
  5. return ''; 
  6.  
  7. // Just to make things a little easier, pad the end. 
  8. $pee = $pee . "\n"; 
  9.  
  10. /** 
  11. * Pre tags shouldn't be touched by autop. 
  12. * Replace pre tags with placeholders and bring them back after autop. 
  13. */ 
  14. if ( strpos($pee, '<pre') !== false ) { 
  15. $pee_parts = explode( '</pre>', $pee ); 
  16. $last_pee = array_pop($pee_parts); 
  17. $pee = ''; 
  18. $i = 0; 
  19.  
  20. foreach ( $pee_parts as $pee_part ) { 
  21. $start = strpos($pee_part, '<pre'); 
  22.  
  23. // Malformed html? 
  24. if ( $start === false ) { 
  25. $pee .= $pee_part; 
  26. continue; 
  27.  
  28. $name = "<pre wp-pre-tag-$i></pre>"; 
  29. $pre_tags[$name] = substr( $pee_part, $start ) . '</pre>'; 
  30.  
  31. $pee .= substr( $pee_part, 0, $start ) . $name; 
  32. $i++; 
  33.  
  34. $pee .= $last_pee; 
  35. // Change multiple <br>s into two line breaks, which will turn into paragraphs. 
  36. $pee = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee); 
  37.  
  38. $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)'; 
  39.  
  40. // Add a double line break above block-level opening tags. 
  41. $pee = preg_replace('!(<' . $allblocks . '[\s/>])!', "\n\n$1", $pee); 
  42.  
  43. // Add a double line break below block-level closing tags. 
  44. $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee); 
  45.  
  46. // Standardize newline characters to "\n". 
  47. $pee = str_replace(array("\r\n", "\r"), "\n", $pee); 
  48.  
  49. // Find newlines in all elements and add placeholders. 
  50. $pee = wp_replace_in_html_tags( $pee, array( "\n" => " <!-- wpnl --> " ) ); 
  51.  
  52. // Collapse line breaks before and after <option> elements so they don't get autop'd. 
  53. if ( strpos( $pee, '<option' ) !== false ) { 
  54. $pee = preg_replace( '|\s*<option|', '<option', $pee ); 
  55. $pee = preg_replace( '|</option>\s*|', '</option>', $pee ); 
  56.  
  57. /** 
  58. * Collapse line breaks inside <object> elements, before <param> and <embed> elements 
  59. * so they don't get autop'd. 
  60. */ 
  61. if ( strpos( $pee, '</object>' ) !== false ) { 
  62. $pee = preg_replace( '|(<object[^>]*>)\s*|', '$1', $pee ); 
  63. $pee = preg_replace( '|\s*</object>|', '</object>', $pee ); 
  64. $pee = preg_replace( '%\s*(</?(?:param|embed)[^>]*>)\s*%', '$1', $pee ); 
  65.  
  66. /** 
  67. * Collapse line breaks inside <audio> and <video> elements,  
  68. * before and after <source> and <track> elements. 
  69. */ 
  70. if ( strpos( $pee, '<source' ) !== false || strpos( $pee, '<track' ) !== false ) { 
  71. $pee = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $pee ); 
  72. $pee = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $pee ); 
  73. $pee = preg_replace( '%\s*(<(?:source|track)[^>]*>)\s*%', '$1', $pee ); 
  74.  
  75. // Collapse line breaks before and after <figcaption> elements. 
  76. if ( strpos( $pee, '<figcaption' ) !== false ) { 
  77. $pee = preg_replace( '|\s*(<figcaption[^>]*>)|', '$1', $pee ); 
  78. $pee = preg_replace( '|</figcaption>\s*|', '</figcaption>', $pee ); 
  79.  
  80. // Remove more than two contiguous line breaks. 
  81. $pee = preg_replace("/\n\n+/", "\n\n", $pee); 
  82.  
  83. // Split up the contents into an array of strings, separated by double line breaks. 
  84. $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY); 
  85.  
  86. // Reset $pee prior to rebuilding. 
  87. $pee = ''; 
  88.  
  89. // Rebuild the content as a string, wrapping every bit with a <p>. 
  90. foreach ( $pees as $tinkle ) { 
  91. $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n"; 
  92.  
  93. // Under certain strange conditions it could create a P of entirely whitespace. 
  94. $pee = preg_replace('|<p>\s*</p>|', '', $pee); 
  95.  
  96. // Add a closing <p> inside <div>, <address>, or <form> tag if missing. 
  97. $pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee); 
  98.  
  99. // If an opening or closing block element tag is wrapped in a <p>, unwrap it. 
  100. $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); 
  101.  
  102. // In some cases <li> may get wrapped in <p>, fix them. 
  103. $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); 
  104.  
  105. // If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>. 
  106. $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee); 
  107. $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee); 
  108.  
  109. // If an opening or closing block element tag is preceded by an opening <p> tag, remove it. 
  110. $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee); 
  111.  
  112. // If an opening or closing block element tag is followed by a closing <p> tag, remove it. 
  113. $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); 
  114.  
  115. // Optionally insert line breaks. 
  116. if ( $br ) { 
  117. // Replace newlines that shouldn't be touched with a placeholder. 
  118. $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee); 
  119.  
  120. // Normalize <br> 
  121. $pee = str_replace( array( '<br>', '<br/>' ), '<br />', $pee ); 
  122.  
  123. // Replace any new line characters that aren't preceded by a <br /> with a <br />. 
  124. $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); 
  125.  
  126. // Replace newline placeholders with newlines. 
  127. $pee = str_replace('<WPPreserveNewline />', "\n", $pee); 
  128.  
  129. // If a <br /> tag is after an opening or closing block tag, remove it. 
  130. $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee); 
  131.  
  132. // If a <br /> tag is before a subset of opening or closing block tags, remove it. 
  133. $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee); 
  134. $pee = preg_replace( "|\n</p>$|", '</p>', $pee ); 
  135.  
  136. // Replace placeholder <pre> tags with their original content. 
  137. if ( !empty($pre_tags) ) 
  138. $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee); 
  139.  
  140. // Restore newlines in all elements. 
  141. if ( false !== strpos( $pee, '<!-- wpnl -->' ) ) { 
  142. $pee = str_replace( array( ' <!-- wpnl --> ', '<!-- wpnl -->' ), "\n", $pee ); 
  143.  
  144. return $pee;