Post Formats in WordPress: Breaking Down Those Quotes

Here’s a function that would grab the contents HTML and parse out the first quote (blockquote element) together with it’s cite (usually the quote author) and the remaining text that comes after the quote. This is useful for when dealing with the quote post format in WordPress.

function get_blockquote() {
    $dom = new DOMDocument;
    $dom->loadHTML( apply_filters( 'the_content', get_the_content( '' ) ) );
    $blockquotes = $dom->getElementsByTagname( 'blockquote' );

    if ( $blockquotes->length > 0 ) {

        // First blockquote
        $blockquote = $blockquotes->item(0);

        $cite = $blockquote->getElementsByTagName( 'cite' )->item( 0 );
        $p = $blockquote->getElementsByTagName( 'p' );

        $cite_content = '';
        if ( $cite && $p ) {

            // Remove the cite from the paragraph
            foreach ( $p as $paragraph )
                try { $paragraph->removeChild( $cite ); }
                catch( Exception $e ) {}

            $cite_content = $dom->saveXML( $cite );
        }

        $blockquote_content = '';
        foreach ( $p as $paragraph ) {
            if ( strlen( trim( $paragraph->nodeValue ) ) > 0 )
                $blockquote_content .= $dom->saveXML( $paragraph );
            else
                $paragraph->parentNode->removeChild( $paragraph );

        $blockquote->parentNode->removeChild( $blockquote );
        $remaining_content = $dom->saveXML();
    }
    return $blockquote_content; // $cite_content or $remaining_content
}

As you can see you have an option of returning the quote contents, the author contents or the stuff that’s not related to the first quote. You can also bundle all three of them in an array if needed. Anyways, as I said, this might come useful when dealing with the quote post format in WordPress.

Quote post format in WordPress

We’ve got a great example on Theme.fm and the best part is that you don’t need to change the way you write quotes and since you’ve got the quote content, the author and the rest of the content as separate entities, you can style your first (main) quote however you like.

This continues my discussion on Tumblr-like Post Formats by Alex King and Crowd Favorite. And obviously the code snippet can be improved, but hey, who needs regular expressions? ;)

2 thoughts on “Post Formats in WordPress: Breaking Down Those Quotes

Comments are closed.