How to Show ads before heading Tag in WordPress without Plugins

Hey! today i share code to show ads before heading tag, You can add given code with ads code in functions.php and show ads before heading tag like – h1, h2, h3, h4, h5, h6 .

How to Show ads Before Heading Tag

  • open theme and go to active theme’s functions.php file.
  • then paste given code and SAVE.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-6761945224741025"
crossorigin="anonymous"></script>
<ins class="adsbygoogle"
style="display:block; text-align:center;"
data-ad-layout="in-article"
data-ad-format="fluid"
data-ad-client="ca-pub-xxxxxxxxxxxxxx"
data-ad-slot="xxxxxxxxxx"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>';
if ( is_single() ) {
return prefix_insert_after_paragraph( $ad_code, 1, $content );
}
return $content;
}
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_p = '<h3>';
$paragraphs = explode( $closing_p, $content );
if ($paragraphs) {
$paragraphs[0] = $paragraphs[0].$insertion;
}
return implode( '<h3>', $paragraphs );
}

Hope this code will work perfectly in your WordPress theme. If you have any other type help so contact me via contact us form.