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

Must Read
WordPress Dynamic XML Sitemap Without Plugin

WordPress Dynamic XML Sitemap Without Plugin After logging into WordPress dashboard go to Appearance > Editor from the menu at Read more

Remove query strings from static resources without wordpress plugin

Way toRemove query strings from static resources without wordpress plugin After logging into WordPress dashboard go to Appearance > Editor Read more

Add Defer and Async Attributes to Render Blocking Javascript in wordpress without plugin

How to Add Defer and Async Attributes to Render Blocking Javascript After logging into WordPress dashboard go to Appearance > Read more

How to Remove the […] from the excerpt without plugin

Steps to Remove the [...] from the excerpt without plugin After logging into WordPress dashboard go to Appearance > Editor from Read more

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

 

Leave a Reply

Your email address will not be published. Required fields are marked *