Skip to content

Instantly share code, notes, and snippets.

@boonebgorges
Created September 6, 2012 15:54
Show Gist options
  • Save boonebgorges/3657745 to your computer and use it in GitHub Desktop.
Save boonebgorges/3657745 to your computer and use it in GitHub Desktop.
WP filter to turn email-style quotes into blockquotes
<?php
/**
* Turn email-style quotes into blockquotes
*
* For example:
*
* My friend said:
*
* > You are handsome and also
* > you are very great
*
* Then she said:
*
* > You are my hero
*
* becomes
*
* My friend said:
*
* <blockquote>You are handsome and also you are very great</blockquote>
*
* Then she said:
*
* <blockquote>You are my hero</blockquote>
*
* This method is neither elegant nor efficient, but it works.
*/
function teleogistic_process_quotes( $content ) {
// Find blank lines
$content = preg_replace( '/\n\s*\n/m', '<BBG_EMPTY_LINE>', $content );
// Explode on the blank lines
$content = explode( '<BBG_EMPTY_LINE>', $content );
foreach ( $content as &$c ) {
$c = trim( $c );
// Reduce multiple-line quotes to a single line
// This works because the first > in a block will not have a
// line break before it
$c = preg_replace( '/\n(>|&gt;)(.*)/m', '$2', $c );
// Blockquote 'em
$c = preg_replace( '/^(>|&gt;) (.*)/m', '<blockquote>$2</blockquote>', $c );
}
// Put everything back as we found it
$content = implode( "\n\n", $content );
return $content;
}
add_filter( 'the_content', 'teleogistic_process_quotes', 5 );
add_filter( 'get_comment_text', 'teleogistic_process_quotes', 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment