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. // Remove more than two contiguous line breaks. 
  76. $pee = preg_replace("/\n\n+/", "\n\n", $pee); 
  77.  
  78. // Split up the contents into an array of strings, separated by double line breaks. 
  79. $pees = preg_split('/\n\s*\n/', $pee, -1, PREG_SPLIT_NO_EMPTY); 
  80.  
  81. // Reset $pee prior to rebuilding. 
  82. $pee = ''; 
  83.  
  84. // Rebuild the content as a string, wrapping every bit with a <p>. 
  85. foreach ( $pees as $tinkle ) { 
  86. $pee .= '<p>' . trim($tinkle, "\n") . "</p>\n"; 
  87.  
  88. // Under certain strange conditions it could create a P of entirely whitespace. 
  89. $pee = preg_replace('|<p>\s*</p>|', '', $pee); 
  90.  
  91. // Add a closing <p> inside <div>, <address>, or <form> tag if missing. 
  92. $pee = preg_replace('!<p>([^<]+)</(div|address|form)>!', "<p>$1</p></$2>", $pee); 
  93.  
  94. // If an opening or closing block element tag is wrapped in a <p>, unwrap it. 
  95. $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); 
  96.  
  97. // In some cases <li> may get wrapped in <p>, fix them. 
  98. $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); 
  99.  
  100. // If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>. 
  101. $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee); 
  102. $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee); 
  103.  
  104. // If an opening or closing block element tag is preceded by an opening <p> tag, remove it. 
  105. $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee); 
  106.  
  107. // If an opening or closing block element tag is followed by a closing <p> tag, remove it. 
  108. $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); 
  109.  
  110. // Optionally insert line breaks. 
  111. if ( $br ) { 
  112. // Replace newlines that shouldn't be touched with a placeholder. 
  113. $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', '_autop_newline_preservation_helper', $pee); 
  114.  
  115. // Normalize <br> 
  116. $pee = str_replace( array( '<br>', '<br/>' ), '<br />', $pee ); 
  117.  
  118. // Replace any new line characters that aren't preceded by a <br /> with a <br />. 
  119. $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); 
  120.  
  121. // Replace newline placeholders with newlines. 
  122. $pee = str_replace('<WPPreserveNewline />', "\n", $pee); 
  123.  
  124. // If a <br /> tag is after an opening or closing block tag, remove it. 
  125. $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee); 
  126.  
  127. // If a <br /> tag is before a subset of opening or closing block tags, remove it. 
  128. $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee); 
  129. $pee = preg_replace( "|\n</p>$|", '</p>', $pee ); 
  130.  
  131. // Replace placeholder <pre> tags with their original content. 
  132. if ( !empty($pre_tags) ) 
  133. $pee = str_replace(array_keys($pre_tags), array_values($pre_tags), $pee); 
  134.  
  135. // Restore newlines in all elements. 
  136. if ( false !== strpos( $pee, '<!-- wpnl -->' ) ) { 
  137. $pee = str_replace( array( ' <!-- wpnl --> ', '<!-- wpnl -->' ), "\n", $pee ); 
  138.  
  139. return $pee;