How to insert ads into your wordpress posts content

The easiest way to add advertisements into your wordpress posts in my opinion is to use the function.php file. When you login to wordpress you go to Appearance and then Editor.

Down the right side of the Editor page you will need to find a file called either theme functions, if you can’t find that look for functions.php and open it.

Once you have opened that file you will see this or something similar:

Wordpress Theme Functions

You will be able to insert the code below into that file, make sure you take a backup of the file just in case. Also make sure you add the following code below the otherwise it will not work.

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {
	$ad_code = '<div>Ads code goes here</div>';

	if ( is_single() && ! is_admin() ) {
		return prefix_insert_after_paragraph( $ad_code, 2, $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 );

Where it says “Ads code goes here” you can just paste all your google adsense code in there. Where is says “$paragraph_id == $index 1” you can change 1 to which every paragraph you want to show you ad after.


I am a freelance website designer, this is my blog website were I like to post freebie website templates and scripts and other cool useful stuff.

You may also like...