Insert Ads After The First Paragraph Of Your Post

This PHP snippet automatically inserts your Ads after the first paragraph found in your post content.

How to use it:

1. Login to your WordPress admin panel.

2. Click the Editor link under the Appearance section of the dashboard.

Edit WordPress Template

3. Click the Theme Functions link on the right hand side of the Edit Themes page.

Edit Theme Functions

4. Copy and add the following PHP snippets to the end of the functions.php.


add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {

$ad_code = 'YOUT ADS CODE HERE';

if ( is_single() && ! is_admin() ) {
  return prefix_insert_after_paragraph( $ad_code, 1, $content );
}

return $content;
}

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_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 );
}

5. Click the Update File button and done.

Update Theme File

Tags: ,

Leave a Reply