Finding the paragraphs in content

The paragraphs are done by the wpautop() function, hooked to the_content, the_excerpt() & comment_text as well as 'term_description' for taxonomies.


The plugin linked by @javipas does an enormous effort to just add this, but it’s a good example (+1). You can (modify it a little and) take the following part out of it:

// The init function
function wpse24553_add_p_the_content()
{
    add_filter( 'the_content', 'wpse24553_p_the_content' );
    add_filter( 'the_content_feed', 'wpse24553_p_the_content' );
}
add_action( 'init', 'wpse24553_add_p_the_content' );

// The actual modification function
function wpse24553_p_the_content( $the_content )
{
    global $post;

    $content_by_p = preg_split( '/<\/p>/is', $the_content );

    $i = 0;
    // Set a var to count until the targeted <p> is met - change this to your needs
    // Set to empty '' if you want to modify every paragraph
    $targeted_p = 1;

    static $new_content="";

    foreach ( $content_by_p as $key => $p )
    {
        $i++;
        // abort and return the modified content if we're beyond the targeted <p>
        if ( $i > $targeted_p )
            {
            $new_content .= $p;
                    continue;
            }

        // Remove empty space at the end of a paragraph, then remove original <p>-tag
        $p = rtrim( $p );
        $p = preg_replace( '/<p>/is', '', $p );

        // Wrap replacements in new <p>-tags, so it validates
        $new_content .= '<p class="paragraph-link"><a name="p-'.$key.'"></a>';
        // Prepend the graf with an anchor tag
        $new_content .= '<a ref="permalink" title="Permalink to this paragraph" href="'.get_permalink( $post->ID ).'#p-'.$key.'">#</a>;
        $new_content .= $p;
        $new_content .= '</p>';
    }

    // Return the new content
    return $new_content;
}

Notes:

  • The function needs to be placed in your functions.php
  • You need to alter the function and what gets added/removed/modified with a single paragraph by yourself (no use case in the Q).
  • The function currently is not tested.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)