Web & Software Developer

Insert Google Adsense Native In-article ad on Regular WordPress Posts, and AMP ad on AMP pages

This is one code snippet to insert Google Adsense ads on all your WordPress posts, both regular and AMP. (This code works with the official AMP for WordPress plugin.) This one code snippet will automatically insert one Google Adsense ad after the first paragraph of every post. On regular posts, it will insert a “Native In-article ad.” On AMP posts, it will insert an AMP-compatible ad.

To make this work:

  • Replace ca-pub-1234567891234567 on line 6 with your own Adsense publisher ID.
  • Replace 1234567890 on line 7 with the ad ID of your Native In-article ad code.
  • Replace 1234567890 on line 8 with the ad Id of your Responsive ad code.

/**
 * Returns a Native In-article ad on regular pages, or an AMP ad on AMP pages
 */
function isa_adsense_native_code( $atts = array() ) {

	$publisher_id = 'ca-pub-1234567891234567';
	$ad_id = '1234567890'; // Native In-article ad code ID
	$amp_ad_id = '1234567890'; // Responive ad code ID for AMP

	if ( is_amp_endpoint() ) {
		// AMP
		return '<amp-ad width="100vw" height=320 type="adsense" data-ad-client="' . $publisher_id . '" data-ad-slot="' . $amp_ad_id . '" data-auto-format="rspv" data-full-width><div overflow></div></amp-ad>';
	} else {
		return '<ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="' . $publisher_id . '" data-ad-slot="' . $ad_id . '"></ins><script>(adsbygoogle = window.adsbygoogle || []).push({});</script>';
	}
}

/**
 * Insert ad into content after paragraph 1, for both regular and AMP
 */
function isa_insert_ads($content) {
	if ( is_single() && ! is_admin() ) {
	    $ad_code = isa_adsense_native_code();
    	$new_content = isa_insert_after_paragraph( $ad_code, 1, $content );
 	    return $new_content;
	}
    return $content;
}
add_filter('the_content', 'isa_insert_ads');
function isa_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 );
}

/**
 * Add the regular Adsense script to regular single posts.
 */
function isa_load_regular_adsense_script() {
    wp_register_script( 'google-adsense', '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js', '', null, true );
    if ( is_singular() ) {
        wp_enqueue_script( 'google-adsense' );
    }
}
add_action( 'wp_enqueue_scripts', 'isa_load_regular_adsense_script' );

/**
 * Add async attribute to the regular Adsense script.
 */
function isa_adsense_async_script( $tag, $handle, $src ) {
    if ( 'google-adsense' == $handle ) {
        return '<script async src="' . $src . '"></script>';
    }
    return $tag;
}
add_filter( 'script_loader_tag', 'isa_adsense_async_script', 10, 3 );

/**
 * Add the AMP Ad script to AMP pages.
 */
function isa_amp_ad_script( $data ) {
    $data['amp_component_scripts']['amp-ad'] = 'https://cdn.ampproject.org/v0/amp-ad-0.1.js';
    return $data;
}
add_filter( 'amp_post_template_data', 'isa_amp_ad_script' );

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]