Categories
GIDBlog Learning Journal

WordPress Plugin for Google AdSense Auto Ads

I will show you how I inserted Google AdSense Auto Ads JavaScript ad code to this web site today.

The Google AdSense Auto Ads ad code is only a single line long, so it was unnecessary to get overly complicated with the plugin or PHP code.

Create file: gid-wp-google-adsense-auto-ads.php

Using my favourite text/PHP editor, I created a PHP file: gid-wp-google-adsense-auto-ads.php. This is how it looks:

<?php
/*
Plugin Name: GID WP Google AdSense Auto Ads
Plugin URI: https://www.gidblog.com/wordpress-plugin-google-adsense-auto-ads/
Description: Inserts Google AdSense Auto Ads, in the header.
Author: J de Silva
Version: 1.0
Author URI: https://www.gidblog.com/author/jdesilva/
*/

// Replace "NNNNNNNNNNNNNNNN" with your Google AdSense Publisher ID (just the 16-digit number)
define( "GID_GA_PUBID", "NNNNNNNNNNNNNNNN" );

function gid_google_adsense_autoads()
{
?>
<script data-ad-client="ca-pub-<?php echo GID_GA_PUBID; ?>" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<?php
}
add_action( 'wp_head', 'gid_google_adsense_autoads', 10 );

Do not forget to paste your own Google AdSense publisher ID (just the 16-digit number) in the PHP code before uploading the file. See line no. 12 above.

Upload gid-wp-google-adsense-auto-ads.php

Save the file and upload it to the /wp-content/plugins/ folder of your WordPress site. On my Linux laptop, with the scp command, this is as simple as:

scp -v $HOME/gid-wp-google-adsense-auto-ads.php [email protected]:/var/www/html/wp-content/plugins/

Finally, go to the Plugins page inside your WordPress site’s admin dashboard to activate this plugin.