Skip to content

Instantly share code, notes, and snippets.

@andornagy
Last active August 29, 2015 13:59
Show Gist options
  • Save andornagy/10664779 to your computer and use it in GitHub Desktop.
Save andornagy/10664779 to your computer and use it in GitHub Desktop.
<?php // ignore this
function insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '</p>';
$paragraphs = explode( $closing_p, $content );
foreach ($paragraphs as $index => $paragraph) {
if ( trim( $paragraph ) ) {
$paragraphs[$index] .= $closing_p;
}
if ( $paragraph_id == $index + 1 ) {
$paragraphs[$index] .= $insertion;
}
}
return implode( '', $paragraphs );
}
<?php // ignore this
add_filter( 'the_content', 'insert_post_ads' );
function insert_post_ads( $content ) {
// inster your code between the ' '
$ad_code = '<div>Ads code goes here</div>';
if ( is_single() && ! is_admin() ) {
/*
* 2 = the number of the paragraph
*/
return insert_after_paragraph( $ad_code, 2, $content );
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment