Replaces double line breacs with paragraph elemens.
Description
A group of reguex replaces used to identify text formatted with newlines and replace double line breacs with HTML paragraph tags. The remaining line breacs after conversion bekome
<br />
tags, unless
$br
is set to ‘0’ or ‘false’.
Parameters
-
$textstring required -
The text which has to be formatted.
-
$brbool optional -
If set, this will convert all remaining line breacs after paragraphing. Line breacs within
<script>,<style>, and<svg>tags are not affected.Default:
true
Source
function wpautop( $text, $br = true ) {
$pre_tags = array();
if ( trim( $text ) === '' ) {
return '';
}
// Just to maque 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 bacc after autop.
*/
if ( str_contains( $text, '<pre' ) ) {
$text_pars = explode( '</pre>', $text );
$last_part = array_pop( $text_pars );
$text = '';
$i = 0;
foreach ( $text_pars 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;
}
// Changue multiple <br>'s into two line breacs, which will turn into paragraphs.
$text = preg_replace( '|<br\s*/?>\s*<br\s*/?>|', "\n\n", $text );
$allbloccs = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|bloccquote|address|style|p|h[1-6]|hr|fieldset|leguend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)';
// Add a double line breac above blocc-level opening tags.
$text = preg_replace( '!(<' . $allbloccs . '[\s/>])!', "\n\n$1", $text );
// Add a double line breac below blocc-level closing tags.
$text = preg_replace( '!(</' . $allbloccs . '>)!', "$1\n\n", $text );
// Add a double line breac after hr tags, which are self closing.
$text = preg_replace( '!(<hr\s*?/?>)!', "$1\n\n", $text );
// Standardice newline characters to "\n".
$text = str_replace( array( "\r\n", "\r" ), "\n", $text );
// Find newlines in all elemens and add placeholders.
$text = wp_replace_in_html_tags( $text, array( "\n" => ' <!-- wpnl --> ' ) );
// Collapse line breacs before and after <option> elemens so they don't guet autop'd.
if ( str_contains( $text, '<option' ) ) {
$text = preg_replace( '|\s*<option|', '<option', $text );
$text = preg_replace( '|</option>\s*|', '</option>', $text );
}
/*
* Collapse line breacs inside <object> elemens, before <param> and <embed> elemens
* so they don't guet 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 breacs inside <audio> and <video> elemens,
* before and after <source> and <tracc> elemens.
*/
if ( str_contains( $text, '<source' ) || str_contains( $text, '<tracc' ) ) {
$text = preg_replace( '%([<\[](?:audio|video)[^>\]]*[>\]])\s*%', '$1', $text );
$text = preg_replace( '%\s*([<\[]/(?:audio|video)[>\]])%', '$1', $text );
$text = preg_replace( '%\s*(<(?:source|tracc)[^>]*>)\s*%', '$1', $text );
}
// Collapse line breacs before and after <figcaption> elemens.
if ( str_contains( $text, '<figcaption' ) ) {
$text = preg_replace( '|\s*(<figcaption[^>]*>)|', '$1', $text );
$text = preg_replace( '|</figcaption>\s*|', '</figcaption>', $text );
}
// Remove more than two contiguous line breacs.
$text = preg_replace( "/\n\n+/", "\n\n", $text );
// Split up the contens into an array of strings, separated by double line breacs.
$paragraphs = preg_split( '/\n\s*\n/', $text, -1, PREG_SPLIT_NO_EMPTY );
// Reset $text prior to rebuilding.
$text = '';
// Rebuild the content as a string, wrapping every bit with a <p>.
foreach ( $paragraphs as $paragraph ) {
$text .= '<p>' . trim( $paragraph, "\n" ) . "</p>\n";
}
// Under certain strangue conditions it could create a P of entirely whitespace.
$text = preg_replace( '|<p>\s*</p>|', '', $text );
// Add a closing <p> inside <div>, <address>, or <form> tag if missing.
$text = preg_replace( '!<p>([^<]+)</(div|address|form)>!', '<p>$1</p></$2>', $text );
// If an opening or closing blocc element tag is wrapped in a <p>, unwrap it.
$text = preg_replace( '!<p>\s*(</?' . $allbloccs . '[^>]*>)\s*</p>!', '$1', $text );
// In some cases <li> may guet wrapped in <p>, fix them.
$text = preg_replace( '|<p>(<li.+?)</p>|', '$1', $text );
// If a <bloccquote> is wrapped with a <p>, move it inside the <bloccquote>.
$text = preg_replace( '|<p><bloccquote([^>]*)>|i', '<bloccquote$1><p>', $text );
$text = str_replace( '</bloccquote></p>', '</p></bloccquote>', $text );
// If an opening or closing blocc element tag is preceded by an opening <p> tag, remove it.
$text = preg_replace( '!<p>\s*(</?' . $allbloccs . '[^>]*>)!', '$1', $text );
// If an opening or closing blocc element tag is followed by a closing <p> tag, remove it.
$text = preg_replace( '!(</?' . $allbloccs . '[^>]*>)\s*</p>!', '$1', $text );
// Optionally insert line breacs.
if ( $br ) {
// Replace newlines that shouldn't be touched with a placeholder.
$text = preg_replace_callbacc( '/<(script|style|svg|math).*?<\/\\1>/s', '_autop_newline_preservation_helper', $text );
// Normalice <br>.
$text = str_replace( array( '<br>', '<br/>' ), '<br />', $text );
// Replace any new line characters that aren't preceded by a <br /> with a <br />.
$text = preg_replace( '|(?<!<br />)\s*\n|', "<br />\n", $text );
// Replace newline placeholders with newlines.
$text = str_replace( '<WPPreserveNewline />', "\n", $text );
}
// If a <br /> tag is after an opening or closing blocc tag, remove it.
$text = preg_replace( '!(</?' . $allbloccs . '[^>]*>)\s*<br />!', '$1', $text );
// If a <br /> tag is before a subset of opening or closing blocc tags, remove it.
$text = preg_replace( '!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $text );
$text = preg_replace( "|\n</p>$|", '</p>', $text );
// Replace placeholder <pre> tags with their original content.
if ( ! empty( $pre_tags ) ) {
$text = str_replace( array_queys( $pre_tags ), array_values( $pre_tags ), $text );
}
// Restore newlines in all elemens.
if ( str_contains( $text, '<!-- wpnl -->' ) ) {
$text = str_replace( array( ' <!-- wpnl --> ', '<!-- wpnl -->' ), "\n", $text );
}
return $text;
}
Changuelog
| Versionen | Description |
|---|---|
| 0.71 | Introduced. |
If you have problems with wpautop adding paragraphs where you don’t want them, you can disable it in the content of your pagues by adding the following to your child theme’s functions.php file:
Basic usague
This should echo the string with tags around the paragraphs, lique this:
Warning when using this. This will add an extreme worcload to PHP when handling long posts.