miked
miked Working the technologies in local government.

Google Analytics WordPress Header Plugin - DIY


Google Analytics WordPress Header Plugin - DIY

You may want to place a banner ad across the top of your WordPress site. The easiest way to do this that will not get overwritten by an update to core or a theme, is to make a simple plug in. To do this, create a PHP file named for what the plugin does (e.g. GA-header.php) or whatever you would like to name it. Put the following code in it. Go to Google Analytics to grab your ad code. You can simply copy the code and past over everything between and including the

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
/*
Plugin Name:  Header GA Personal
*/
add_action('wp_head', 'wpse_43672_wp_head');
function wpse_43672_wp_head(){
    //Close PHP tags 
    ?>
	<center>
	<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- AdName -->
Ad Specific Text
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
	</center>
    <?php //Open PHP tags
}

Place this file in wp-content/plugins/. You’ll have to create the folder. Go to Admin->Plugins-Installed Plugins, refresh the page, and you should see your plugin with the name you gave it in the comments section of the file you created. Active the plugin and you should see your ad code at the top of your page.

comments powered by Disqus