Blocks::reverse_wpautop( string $content )

Reverses the effects of wpautop by converting HTML paragraph and break tags back into plain text line breaks.

This function aims to revert the automatic formatting applied by WordPress’s wpautop function, which converts double line breaks in text into paragraph elements and single line breaks into
tags.


Parameters

$content

(string) (Required) The HTML content to process, typically containing <p>, </p>, and <br> tags.


Top ↑

Return

(string) The processed content.


Top ↑

Source

File: src/Events_Community/Block_Conversion/Blocks.php

	public function reverse_wpautop( string $content ): string {
		// Ensures that the content starts and ends with paragraph tags for consistent processing.
		$content = '<p>' . $content . '</p>';

		$content = preg_replace( '/<p[^>]*>/', "\n\n", $content ); // Replace opening <p> tags with two newlines.
		$content = str_replace( '</p>', '', $content ); // Remove closing </p> tags.
		$content = preg_replace( '/<br ?\/?>/', "\n", $content ); // Replace <br> tags with a single newline.
		$content = preg_replace( "/(\n\s*){3,}/", "\n\n", $content ); // Reduce three or more newlines to two newlines.

		return trim( $content ); // Trim whitespace from the beginning and end.
	}

Top ↑

Changelog

Changelog
Version Description
4.10.17 Introduced.