I want to show ads by Google between text content, like show 3 ads one after 1st para second after 3rd para and one at end of last para.

I know i have to modify in single.php but being novice in php I am unable to do this.

Would be real helpful if someone could help me do this. Thanks.

  • As you need to put it inside content. I think it would be easier to do it via plugin or shortcode.
    – Sisir
    Apr 23, 2014 at 6:23
  • 1
    You going to kill your content with that much ads in between your content. Why so much google ads in one post? Apr 23, 2014 at 6:25
  • @PieterGoosen I would probably put just one, but would want to know the solution and try this out. Apr 23, 2014 at 6:53

2 Answers 2


You can use a shortcode or the_content filter. I think the_content filter is better because your don't introduce any string in your post, so the content can be exported and used in another platforms if needed. For emxample, for show a block of adsense after the first parragraph:

 add_filter( 'the_content', 'tbn_ads_inside_content' );
 function tbn_ads_inside_content( $content ) {
      $output = $content;
      //We don't want to modify the_content in de admin area
      if( !is_admin() ) {
          $ads = "<p>your_ads_code</p>";
          $p_array = explode('</p>', $content );
          $p_count = 1;

          if( !empty( $p_array ) ){

              array_splice( $p_array, $p_count, 0, $ads );
              $output = '';

              foreach( $p_array as $key=>$value ){

                  $output .= $value;



      return $output;

  • 1
    Nice, short and sweet. Might actually use your solution from now on. +1 Apr 23, 2014 at 8:15

Here is the function I use to add a widget after the second paragraph in a post. This is only for the first post and single posts, so you will need to tweak it a bit if you need to show this in all posts. You should be able to modify this to suit your needs. Hope this helps

// Add advertising widget within the firts post and single posts
function pietergoosen_insert_content_after_second_paragraph_filter( $content ) {
    static $first;
    $first = (!isset($first)) ? true : false;
    if (true == $first) {
      echo '<div class="widget-box">';
         dynamic_sidebar( 'sidebar-19' );
         echo '</div><!-- end .widget-box -->';

      $new_content = ob_get_clean();
      if ( ! is_admin() ) {
          return pietergoosen_insert_content_after_second_paragraph( $new_content, 2, $content );
    return $content;

// Paragraph explode and insert
function pietergoosen_insert_content_after_second_paragraph( $new_content, $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] .= $new_content;
    return implode( '', $paragraphs );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.