[Support request] Amp Auto ads and adsense code inside content

Home Forums Support [Support request] Amp Auto ads and adsense code inside content

Home Forums Support Amp Auto ads and adsense code inside content

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1163656
    Khaled

    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”&gt; </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
    }

    #1164318
    Tom
    Lead Developer
    Lead Developer

    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?

    #1164342
    Khaled

    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

    #1164658
    Tom
    Lead Developer
    Lead Developer

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

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

    #1164778
    Khaled

    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 } ?>’;

    #1165562
    Tom
    Lead Developer
    Lead Developer

    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 } ?>
    #1165834
    Khaled

    $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 );
    }

    #1165841
    Suraj Katwal

    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

    #1166346
    Tom
    Lead Developer
    Lead Developer

    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;
    } );
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.