Menu Close

In the middle of the text Automatically Insert AdSense into specific paragraphs Code

Adsense To increase your clickthrough rate (CTR), you should place your ad on a more appropriate location and make it visible to the user. There are a number of ways to do that, including color adjustment, numbering, and placement, but the most effective way to do this is to insert AdSense into the body of the story (naturally, read through the text).

As a result, many operators are putting AdSense into the text, and they often find themselves putting ad code every time they write. Of course, this method has the advantage of being able to plant an optimized advertisement in the most optimized position for each article, but it is impossible to change, add or delete the advertisement size or type in future, You have to do your work manually. If you have experienced this kind of situation directly like Gimmeon, you can see how lethal it is.

To make sure this does not happen, and for more active and efficient settings, let's take a look at how you can use code to automatically insert AdSense behind a paragraph in the body of your text.

    Code that identifies paragraphs and puts Adsense in the middle

First, let's start with the completed code

add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code1 = 'Adsense Code';
$ad_code2 = 'Adsense Code';

if ( is_single() && ! is_admin() ) {
 $content = prefix_insert_after_paragraph( $ad_code1, 10, $content );
 $content = prefix_insert_after_paragraph( $ad_code2, 20, $content );
 return $content;
return $content;

function prefix_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 );

    Code Description and Usage

The code shows what it is and how to use it.

  • Underlined in bluead_code1Red behindAdsense CodeRemove the section and insert your AdSense ad code.
  • The red number below10Section determines which paragraphs the ad will appear after. That is, if 10 is written,</p>At the end of paragraph 10ad_code1InAdsense CodeTo be output.
  • You can add new elements indefinitely by adding ad_code3, ad_code4 to the bottom line like this:
  • This code is optimized for inserting ads like AdSense, but of course, it's an attractive one that can be used in other ways, not just by putting ads. This code is currentlyfunctionsIt is only available on sites like WordPress where you can use filters.

    In the Tistory, you can also put ads under the image through JavaScript. I think you can do it by paragraph because you can put filters by separating images. I will discuss this part later if I have a chance.

  • AdSense URL Override Ad Honeytip, How to make it, Automatically resize image
  • Response AdSense Maximum width width: 100% How to set the largest
  • Google AdSense Publisher Toolbar Error Resolution. Install Google Publisher Toolbar
  • Leave a Reply

    Your email address will not be published.

    Posted in All, Wordpress, Make money

    이메일 구독 - Email Subs

    최선을 다해 직접 만든 콘텐츠만 공유합니다.
    We share the best content we have created.