SHARE

Insert Adsense Function code For wordpressYou can insert Adsense Code in WordPress blog mainly three ways. One way by using plugins for adsense insert in post. Second way insert ad manually in single.php file.And third way is insert ad using a function in function.php.In this article learn how to insert ad with using function.

Way To Insert Adsense Code Using Function.php

Functions.php File In WordPress
Functions.php File In WordPress

To add ad code in WordPress paragraph.Go to WordPress dashboard.In the menu find “Appearance” tab and click on it.Know go to “Editor”.In this section theme file are shown in right side of screen.All the files are in .php format.Find function.php file and click on it.Function.php file is function file of WordPress theme.Copy the following code and paste it inside function.php file. This code in placed anywhere after ” <?php ”  and before ” ?> ” .

Function to insert Adsense Code

Code start here:

//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 = ‘<div class=”ads”>Ads<script async src=”//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>
<!– post1 –>
<ins class=”adsbygoogle”
style=”display:block”
data-ad-client=”ca-pub-8557232931479259″
data-ad-slot=”9046667321″
data-ad-format=”auto”></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></div>’;

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

return prefix_insert_after_paragraph( $ad_code, 3, $content );
}
return $content;
}

Code ends here:

Copy this code or download code  “ //Insert ads after ………….return $content;} “. Paste this code in function.php as define above.To Make this function working copy your adsense ad code from your adsense account.

Get Adsense Code
Get Adsense Code

Replace this function’s highlighted code with your adsense code.Code is looks like is start from” <script ……. </script>“.

Change Ad Location in Post: In this code highlighted 3 (three) number  is the number of paragraph.It means ad shows after third paragraph of the post.If you want to display ad after first paragpraph than change the number to 1 . After done all click update file to save it.

More more help to insert adsense code between the post see the video.