WordPress is the best solution for many of bloggers and site builders because it provides a flexible, extendable, and quick way to build robust websites that non-technical person could manage on their own.
About Wpautop() function
The wpautop function is used to automatically formats posts in WordPress.Wpautop is especially used to automatic formatting a paragraph.Wpautop filter which will change double line breaks into paragraph tags.Whenever you add content using TinyMCE editor and add a new line then will create a double line break which might not look better so this filter solves this problem.
First of all, Let’s understand wpautop if you are confused about what this function does and how to use it.
For example, you have typed following line of code into visual editor on a post
1 2 3 4 | I like creativedev. It is a very nice blog. |
Output:
1 2 3 4 | <p>I like creativedev.</p> <p>It is very nice blog.</p> |
Read: To add new URL rewrite rule in WordPress
Here, I am going to share function which is used to reverse the Wpautop functionality in WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function reverse_wpautop( $string = '' ) { /* return if string is empty */ if ( trim( $string ) === '' ) return ''; /* remove all new lines & <p> tags */ $string = str_replace( array( "\n", "<p>" ), "", $string ); /* replace <br /> with \r */ $string = str_replace( array( "<br />", "<br>", "<br/>" ), "\r", $string ); /* replace </p> with \r\n */ $string = str_replace( "</p>", "\r\n", $string ); /* return clean string */ return trim( $string ); } |
This function is used to reverse the effect of wpautop() function.Permanently removing wpautop is not useful every time so you can use this function and call when you want for turning reversing the “p” and “br” tags to newlines. It will check p and br tags and replaces with the newline character, provides the clean string to you.