Sign In  |  Join Now Become a member to add links and start earning points!

Automatically Add <p> Tags to Content in WordPress

Here’s a nifty trick I learned recently that allows you to automatically add <p> tags to WordPress content. It’s especially useful if you’re working with user comments outside of the usual comment template.

Instead of writing a custom content parser, WordPress has a built in function built specifically for this. By using the wpautop() function, we can take any content, and automatically create <p> tags where there are double line breaks.

As an example of when this is useful, consider displaying the most recent comment on your blog somewhere (maybe in the sidebar, or on a specific page). You might use the following code:

<div id="latest_comment">
    <h1>Latest Comment:</h1>
    <?php
        // Query that selects latest approved comment
        $query = "
            SELECT * FROM $wpdb->comments
            WHERE comment_approved = 1
            ORDER BY comment_date DESC
            LIMIT 1
        ";

        // Get latest comment
        $comments = $wpdb->get_results( $query );

        // Result loop
        foreach( $comments as $comment )
        {
            ?>
            <div class="comment_header">
                <cite><?php echo $comment->comment_author; ?></cite> said:
            </div><!-- .comment_header -->

            <div class="comment_content">
                <?php echo $comment->comment_content; ?>
            </div><!-- .comment_content -->
            <?php
        }
    ?>
</div><!-- #latest_comment -->

Using the above code, when we select the most recent comment, the “comment_content” has no formatting (unless there is inline formatting in the content). This can make some content very difficult to read:

However, if we run “comment_content” through the wpautop() function, we get a much nicer result:

There is only one change in the code. On line 24, we run $comment->comment_content as the argument through the wpautop() function:

<div id="latest_comment">
    <h1>Latest Comment:</h1>
    <?php
        // Query that selects latest approved comment
        $query = "
            SELECT * FROM $wpdb->comments
            WHERE comment_approved = 1
            ORDER BY comment_date DESC
            LIMIT 1
        ";

        // Get latest comment
        $comments = $wpdb->get_results( $query );

        // Result loop
        foreach( $comments as $comment )
        {
            ?>
            <div class="comment_header">
                <cite><?php echo $comment->comment_author; ?></cite> said:
            </div><!-- .comment_header -->

            <div class="comment_content">
                <?php echo wpautop($comment->comment_content); ?>
            </div><!-- .comment_content -->
            <?php
        }
    ?>
</div><!-- #latest_comment -->

This function will work on any string, so it’s not limited to comments. For more information, you can check out wpautop() WordPress Codex page.

8 Comments

  1. Add point Subtract point
    relax tone (1 Point) December 28, 2010 at 2:19 pm

    Ah, wonderful find! Thanks for sharing :).

    Flag as inappropriate
  2. Add point Subtract point
    Website design long Island (3 Points) December 19, 2010 at 9:21 am

    really good source code for database ….

    Flag as inappropriate
  3. Add point Subtract point
    Matthew Kammerer (2 Points) December 15, 2010 at 9:40 am

    Ah, wonderful find! Thanks for sharing :).

    Flag as inappropriate