Insert After Paragraph

<?php
/**
* Insert after Paragraph
* When running on the_content, use priority > 20 so it doesn't affect oEmbed
*
* @param string $insertion
* @param int $paragraph_id
* @param string $content
* @return string $modified_content
*/
function ea_insert_after_paragraph( $insertion, $paragraph_id, $content, $include_at_end = true ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, wpautop( $content ) );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
if( ! $inserted && $include_at_end )
$paragraphs[] = $insertion;
return implode( '', $paragraphs );
}
view raw functions.php hosted with ❤ by GitHub

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk