Created
February 24, 2016 17:58
-
-
Save seoagentur-hamburg/15a60594657623e0239d to your computer and use it in GitHub Desktop.
Einen Google-Werbeblock nach dem ersten Absatz eines Artikels einfügen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* ============================================================================= | |
Google-Werbung nach dem ersten Absatz einfuegen | |
============================================================================= */ | |
add_filter( 'the_content', 'prefix_insert_post_ads' ); | |
function prefix_insert_post_ads( $content ) { | |
$ad_code = '<!-- In Content Google Advertisement --> | |
<div class="content-ads manuell"> | |
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> | |
<!-- In Content Ads --> | |
<ins class="adsbygoogle" | |
style="display:inline-block;width:336px;height:280px" | |
data-ad-client="ca-pub-4412877567706303" | |
data-ad-slot="2668338600"></ins> | |
<script> | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
</script> | |
</div> | |
<!-- END In Content Google Advertisement -->'; | |
if ( is_single() && ! is_admin() ) { | |
return prefix_insert_after_paragraph( $ad_code, 1, $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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment