Skip to content

Instantly share code, notes, and snippets.

@seoagentur-hamburg
Created February 24, 2016 17:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save seoagentur-hamburg/15a60594657623e0239d to your computer and use it in GitHub Desktop.
Save seoagentur-hamburg/15a60594657623e0239d to your computer and use it in GitHub Desktop.
Einen Google-Werbeblock nach dem ersten Absatz eines Artikels einfügen.
<?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