-
-
Save billerickson/84350abfe4b833a4e072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 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
Hey Bill. Where does
$inserted
come from, on line 26?