Insert ads after second paragraph of single post content without wordpress plugin

How to insert ads within your post content after second paragraph in wordpress without wordpress plugin

After logging into WordPress dashboard go to Appearance > Editor from the menu at the left hand side.

Now locate the functions.php file

The location to the file may look like : /home/username/public_html/wp-content/themes/currently_active_theme/functions.php

Make a local backup of this file in case anything goes wrong and you may need to revert to previous stable code.

click on it to begin editing it.

Put this below code in your functions.php to to Insert ads after second paragraph of single post content without wordpress plugin

add_filter( ‘the_content’, ‘prefix_insert_post_ads’ );

function prefix_insert_post_ads( $content ) {

$ad_code = ‘<div>YOUR AD CODE</div>’;

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

 

Note replace above YOUR AD CODE with your adsense code

This will Insert ads after second paragraph of single post content without wordpress plugin

Let me know besumitbe@gmail.com/skype-bpshbp if you have any Query