How to Add Google AdSense Ads Inside Blog Content (Between Paragraphs) Without Plugins

How to Add Google AdSense Ads Inside Blog Content (Between Paragraphs) Without Plugins

Adding Google AdSense codes to your blog or website can be extremely easy with available plugins out there — simply install the plugin, copy and paste codes, and do the tweaks.

However, if you read through reviews and analyses of these plugins, you might get hesitant and disappointed with the downsides usually associated with page load time and conflicts with other installed plugins.

While I have also tried most of these WordPress plugins for running ads on signedMARCO, I decided recently to stick to utilizing the built-in ad slots that come with my premium theme — and integrating some these Google AdSense codes in the functions.php file (Theme Editor) itself so for ads to display even between the single post content or the paragraphs of the running text.

To do the former, simply add the codes to the functions.php file that can be accessed through the Theme Editor (under Appearance tab) of your WordPress site.

In a nutshell, functions.php, or the theme functions file, is a template included in WordPress themes. It acts like a plugin for your WordPress site that is automatically activated with your current theme. This file uses PHP code to add features or change default features on a WordPress site.

Option A: One Google AdSense Code for Repeated Display Ads Between Paragraphs. If you want to use just one set of codes that results in repeated ad displays, e.g., after paragraphs 3, 6, and 9, then you may simply copy the codes below to the functions.php file of your WordPress site.

You may want to add more instances of display by adding new strings ($ad_code4, $ad_code5, and so on). Don’t forget to change the sample ad codes with the actual Google AdSense codes copied from your Google AdSense account and change the paragraph numbers (highlighted) per your preference.

//Insert ads after second paragraph of single post content.

add_filter( ‘the_content’, ‘prefix_insert_post_ads’ );

function prefix_insert_post_ads( $content ) {

$ad_code = ‘YOUR GOOGLE ADSENSE CODE GOES HERE‘;

$ad_code3 = $ad_code2 = $ad_code

if ( is_single() && ! is_admin() ) {

$content = prefix_insert_after_paragraph( $ad_code, 1, $content );

$content = prefix_insert_after_paragraph( $ad_code2, 3, $content );

$content = prefix_insert_after_paragraph( $ad_code3, 9, $content );

return $content;

}

return $content;

}

// Parent Function that makes the magic happen

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

}

 

Option B: Different Google AdSense Codes Between Paragraphs. If you want to easily manage the inventory of your displayed ads, then you might want to have this. There will be a unique code per ad display after each identified paragraph number. Same instructions, but you need individual and unique Google AdSense code per ad slot.

//Insert ads after second paragraph of single post content.

add_filter( ‘the_content’, ‘prefix_insert_post_ads’ );

function prefix_insert_post_ads( $content ) {

$ad_code = ‘YOUR GOOGLE ADSENSE CODE 1 GOES HERE‘;

$ad_code2 = ‘YOUR GOOGLE ADSENSE CODE 2 GOES HERE‘;

$ad_code3 = ‘YOUR GOOGLE ADSENSE CODE 3 GOES HERE‘;

if ( is_single() && ! is_admin() ) {

$content = prefix_insert_after_paragraph( $ad_code, 1, $content );

$content = prefix_insert_after_paragraph( $ad_code2, 3, $content );

$content = prefix_insert_after_paragraph( $ad_code3, 9, $content );

return $content;

}

return $content;

}

// Parent Function that makes the magic happen

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

}

While deciding about the number of ads to display inside the blog content, consider reviewing Google AdSense policies on ad placements to avoid account suspension and deactivation.