It’s a good practice to insert advertisements in WordPress post content. Posting advertisements in WordPress content can be a great way of generating revenue for your website because such ads usually receive high-click through rates. It always recommended to place Google Adsense codes in post content in-case you use the monetization scheme on your blog or rather place affiliate links and banners.

How to post Advertisements in Wordpress post content

Now before we get started, we shall need to add some CSS code on our Style.css for the Div container that is going to hold the advertisement. This container has to be floated either left or right depending on your preference and it’s display has to be changed to inline so that it doesn’t cover the whole line. The height and width may vary depending on your preference.

Copy the code below and add it in your Style.css document.

.adcontainer {
    float: left;
    width: 300px;
    padding: 5px 15px 5px 0px;
    background: none;

Now our code will appear on the left of the content and immediately after it’s block will be the post content.

The next step is all about trimming paragraphs on the content using the Trim() PHP function and where the advert will appear. First of all lets create a function that holds the advertisement code. Please paste these codes in your theme’s functions.php file.

add_filter( 'the_content', 'insert_ads' );

function insert_ads( $postcontent ) {
  $my_code = '<div class="adcontainer">
          // your ad code goes here.....

  if ( ! is_admin() && is_single() ) {
    return insert_code_after_p( $my_code, 1, $postcontent );
  return $postcontent;

In the code above, the function that does the magic is the insert_code_after_p( arg1, arg2, arg3 ) function. The function is illustrated below and it’s the one that trims the post content’s paragraphs them inserts the ad code in it. The function takes 3 arguments which are: your Ad code, the number of the paragraph and the post content.

In the code above the ad code will appear after the first paragraph meaning you can adjust this number to what you want. If you want your ad code to appear after 2 paragraphs then change the number to 2 and so on.

However, the code above cannot work without the code below. Copy and paste this code in your theme’s functions.php file.

function insert_code_after_p( $code_insert, $p_id, $postcontent ) {
  $end_p = '</p>';
  $paragraphs = explode( $end_p, $postcontent );
  foreach ($paragraphs as $index => $paragraph) {

    if ( trim( $paragraph ) ) {
      $paragraphs[$index] .= $end_p;

    if ( $p_id == $index + 1 ) {
      $paragraphs[$index] .= $code_insert;
  return implode( '', $paragraphs );

If you look at the code above you realize this function is part of the function that builds up the first function. It’s the function that trims down the paragraphs and inserts our ad code in them. I hope the code works for you.