Created
September 6, 2012 15:54
-
-
Save boonebgorges/3657745 to your computer and use it in GitHub Desktop.
WP filter to turn email-style quotes into blockquotes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(>|>)(.*)/m', '$2', $c ); | |
// Blockquote 'em | |
$c = preg_replace( '/^(>|>) (.*)/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