Skip to content

Instantly share code, notes, and snippets.

@billerickson

billerickson/functions.php Secret

Last active Feb 23, 2019
Embed
What would you like to do?
<?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;
$inserted = true;
}
}
if( ! $inserted && $include_at_end )
$paragraphs[] = $insertion;
return implode( '', $paragraphs );
}
@JiveDig

This comment has been minimized.

Copy link

@JiveDig JiveDig commented Aug 23, 2016

Hey Bill. Where does $inserted come from, on line 26?

@billerickson

This comment has been minimized.

Copy link
Owner Author

@billerickson billerickson commented Feb 23, 2019

@JiveDig sorry about that, just now saw this.

I added $inserted on line 23. The idea is if the paragraph ID you specify is greater than the number of paragraphs total, then add the insertion at the end of the content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.