Site logo

Archived topic

Amp Auto ads and adsense code inside content

8 replies · Started by Khaled on February 12, 2020

Viewing posts 1–9 of 9

Hi,
I'm still finalizing customizing the theme. I got a couple of issues. For my previous theme, I use the following code in function to insert Adsense after 2nd paragraph. Please have a look and tell me if i need to change anything

//Insert ads after the second paragraph of single post content.

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {

$ad_code = '<!-- Ezoic - article_2nd_paragraph - under_second_paragraph -->

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-xxxxxxxxxxxxx"
data-ad-slot="xxxxxxxxxx"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<!-- End Ezoic - article_2nd_paragraph - under_second_paragraph -->

';

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

Another issue is regarding amp auto ads - For amp auto ads what do i need to change in the following code and insert in function

add_action('amp_post_template_head','amp_my_custom_head');
function amp_my_custom_head($amp_template) {
?>
<script async custom-element="amp-auto-ads" src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js"> </script>
<?php
}

add_action('amp_post_template_footer','amp_my_custom_footer');
function amp_my_custom_footer($amp_template) {
?>
<amp-auto-ads type="adsense"
data-ad-client="ca-pub-xxxxxxxxxxxxx">
</amp-auto-ads>
<?php
}

Hi there,

All of those functions should work in GP just like they did in your previous theme.

Are they not working at the moment? If so, what kind of errors are you getting?

The first function working fine. However, how can i modify the code to output a different amps ad when page is in amp. Like the following - $ad_code = ‘regular Adsense ad code";

to

<?php
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { ?>

<amp-ad code>
</amp-ad>

<?php } else { ?>

Regular ad code

<!-- End Ezoic - Below_article_title - under_page_title -->

<?php } ?>

For the second function to enable amp auto ads not working. But I use the following code which works fine

// ads for amp - will be loaded into header

$print_amp_auto_ads = function() {
$ad_client = 'ca-pub-xxxxxxxxxxxxxx'; // YOUR Publisher ID goes here
if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
?>
<amp-auto-ads type="adsense" data-ad-client="<?php echo esc_attr( $ad_client ); ?>"></amp-auto-ads>
<?php
}
};
// For Paired/Native mode.
add_action( 'wp_footer', $print_amp_auto_ads );

// ads for amp end

That code (is_amp_endpoint()) is exactly how you would do it.

Not seeing anything wrong with the second function, either. Looks good :)

Ok, but placing the code as following break the site.

$ad_code = '<?php
if ( function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint() ) { ?>

<amp-ad code>
</amp-ad>
<?php } else { ?>

Regular ad code
<!– End Ezoic – Below_article_title – under_page_title –>
<?php } ?>';

Why are you using the $ad_code variable?

Try this:

<?php
if ( function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint() ) { ?>

<amp-ad code>
</amp-ad>
<?php } else { ?>

Regular ad code
<!– End Ezoic – Below_article_title – under_page_title –>
<?php } ?>

$ad_code is part of the parent function to execute ad code after first paragraph of the content. I tried the following code but not working --

//Insert ads after the second paragraph of single post content.

add_filter( ‘the_content’, ‘prefix_insert_post_ads’ );

function prefix_insert_post_ads( $content ) {

$ad_code = '<?php
if ( function_exists( ‘is_amp_endpoint’ ) && is_amp_endpoint() ) { ?>

<amp-ad code>
</amp-ad>
<?php } else { ?>

Regular ad code
<!– End Ezoic – Below_article_title – under_page_title –>
<?php } ?>';

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

Here is a code to insert ads after a certain paragraph.

add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = ' Paste your AdSense Code Inside this quote';
if ( is_single() && ! is_admin() ) {
return prefix_insert_after_paragraph( $ad_code, 4, $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 );
}

More at insert Google AdSense Ads in GeneratePress without Plugin

Let's try this, instead:

add_filter( 'the_content', function( $content ) {
    ob_start();

    if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) { ?>
        <amp-ad code>
        </amp-ad>
    <?php } else { ?>
        Regular ad code
        <!– End Ezoic – Below_article_title – under_page_title –>
    <?php }

    $ad_code = ob_get_clean();

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }

    return $content;
} );
This archived topic is closed to new replies.