eightize
Forum Replies Created
-
@mattyrob
Very nice. Thank you. That filter is perfect.Now I’m getting greedy: Is it possible to also add a filter at the end (right before it’s sent to email) so we can clean up multiple newlines, etc? Again, the gutenberg is making things messy: It adds newlines at the end of each of the block wrappers, and between blocks. These are hard to get until all tags have been stripped:
<!-- wp:heading {"level":5} --> <h5>2Heading text</h5> <!-- /wp:heading --> <!-- wp:list {"ordered":true} --> <ol><li>List item 1</li><li>List item 2<br>New line in item 2</li><li>List item 3</li></ol> <!-- /wp:list -->- This reply was modified 6 years, 6 months ago by eightize. Reason: Marked as resolved
@mattyrob Yes, but I think this is related to how the new Gutenberg blocks save the html. In the “classic” it adds newline after every list item, but that’s no longer the case. I did a test with Gutenberg first, then a “classic” block right after. I used <br> inside the list to add separate line.
Here’s how the plain text email message body comes out currently:
Gutenberg blocks Unordered list List item 1List item 2List item 3separate line 3List item 4 ordered list List item 1List item 2List item 3separate line 3List item 4 Classic Unordered list List item 1 List item 2 List item 3separate line 3 List item 4 ordered list List item 1 List item 2 List item 3separate line 3 List item 4Result with the function is a bit cleaner:
Gutenberg blocks Unordered list - List item 1 - List item 2 - List item 3 separate line 3 - List item 4 ordered list 1. List item 1 2. List item 2 3. List item 3 separate line 3 4. List item 4 Classic Unordered list - List item 1 - List item 2 - List item 3 separate line 3 - List item 4 ordered list - List item 1 - List item 2 - List item 3 separate line 3 - List item 4Note all the extra line breaks, also. Gutenberg adds a lot of line breaks — multiple newlines should get a cleanup also at a later point instead of inside this function, but I needed a quick and dirty way to replace <br> with newlines because I used <br> inside an ordered list…
A better version of that ol ul replacement function:
function mjb_list_repl($message, $br_replace=true){ $br="<br>\n"; $ul_replace = '@(<ul[^>]*?>)?<li[^>]*?>(.+?)</li>(</ul>)?@i'; $ol_replace = '@<ol[^>]*?>(.+?)</ol>@i'; $li_replace = '@<li[^>]*?>(.*?)</li>@i'; $ol = array(); // replace ol preg_match_all( $ol_replace, $message, $ol ); if( count($ol)>0 ){ foreach( $ol[1] as $str ){ preg_match_all( $li_replace, $str, $matches ); $repl = ''; if( count( $matches[1] )>0 ){ foreach( $matches[1] as $k=>$v ){ $repl .= ($k+1).". $v$br"; } } $message = preg_replace($ol_replace,$br.$repl,$message,1); } } // replace ul $message = preg_replace( $ul_replace, "$br- $2", $message ); // replace br if( $br_replace ){ // breaks and multiple newlines $message = preg_replace( '@(\r\n|\n){2,}|<br[ /]*>[\n]?@i', "\n", $message ); } return $message; }Is strip_tags() running on the content of the email rather than on the subject of the post and body of the post individually? Seems that wordpress is storing the subject without changing angle brackets to the html entity (I just looked at the database, and it’s stored as an angle bracket, not & lt;). Would it work to run htmlspecialchars() on the subject before including it in the email, then just leave everything else as-is?