Writing Basic Adsense Plugin for WordPress

Fortunately, there are plenty of WordPress plugins available for almost anything. But sometimes, we aren't that lucky, few days ago, I needed an Adsense plugin for a WordPress site, and realized there are way too many plugins for Adsense! I tried few, but I felt they are overly complicated for my need, So, I decided to write my own simple Adsense plugin for WordPress 3. The goal of this tutorial is to create a basic WordPress plugin which will automatically insert Adsense code in each post. For tutorial, We will have a text field to store Adsense code in administrator settings page, and the code will be used to display Adsense Ads in each WordPress post. In the end of the tutorial you will see how easy it can be to create a WordPress plugins, I am sure this simple tutorial will help you create much complicated plugins in future.

Plugin Information

Let's create an empty PHP file in WordPress root/wp-content/plugins/, name it easyadsense.php. All WordPress plugin starts with plugin information enclosed within PHP comment /* */, without this plugin information, WordPress won't recognize the plugin and it won't be available for activation in your WordPress administration page. Let's open the empty file with a PHP editor, and enter our plugin information on top of new plugin file.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
<?php /* Plugin Name: Adsense Easy Plugin URI: https://www.sanwebe.com/2012/10/creating-basic-adsense-plugin-for-wordpress Description: Easily insert Adsense code in WordPress posts Version: 1.0 Author: Saran Chamling Author URI: http://www.sanwebe.com License: License: GPLv2 */ ?>
With plugin information present on top of PHP file, WordPress should be able to recognize it as plugin and find it inside the plugin directory, now the plugin should be listed and ready for activation in your WordPress admin page, but if you activate it, it won't do anything at this stage.

Defines Constants

First thing we want to do when building WordPress plugin is define constants, they may not seem necessary at first, but they will play very useful part later as your plugin size grows and you'll feel more organized. Take a look at code below, I have defined a constant for plugin version, once defined it will be available to everyone, let's say after few months I need to update Plugin with certain versions using a update script, for that I need to know what version it is, so all I need to do now is type "WP_EASY_ADSENSE_VERSION_NUM", effective isn't it?
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
if (!defined('WP_EASY_ADSENSE_NAME')){ //define plugin name define('WP_EASY_ADSENSE_NAME', trim(dirname(plugin_basename(__FILE__)), '/')); } if (!defined('WP_EASY_ADSENSE_PLUGIN_DIR')){ //define plugin dir define('WP_EASY_ADSENSE_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . WP_EASY_ADSENSE_NAME); } if (!defined('WP_EASY_ADSENSE_VERSION_NUM')){ //define plugin version define('WP_EASY_ADSENSE_VERSION_NUM', '1.0'); }

Add a Menu in Admin settings

Once the Plugin gets activated, we need to add a sub-menu in Admin settings menu for Plugin admin page. For that we hook a menu creation function on to "admin_menu" action, and then using add_options_page() we will add sub menu page to the Settings menu. The following code creates our Adsense admin page in WordPress settings.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
add_action('admin_menu', 'wp_easy_adsense_create_page_menu'); //add a menu in admin settings function wp_easy_adsense_create_page_menu() { if (function_exists('add_options_page')) { //add_options_page('Page Title', 'Menu Title', capability, 'Menu slug', 'Create Page Function'); add_options_page(__('Enter Adsense Code for the Post'), 'Adsense Settings', 8, 'easyadsense', 'wp_easy_adsense_settings_page'); } } //Create HTML form in Adsense admin page function wp_easy_adsense_settings_page() { echo '<form method="post" action="options.php" >'; settings_fields( 'wp-easy-adsense-option-group' ); //see register fields in admin page below do_settings_sections( 'wp-easy-adsense' ); //see register fields in admin page below submit_button(); echo '</form>'; }

Register Fields in Admin Page

So far, we have created admin page for our Adsense plugin, but we need input fields too where we allow users to enter and update Adsense JavaScript codes. As you can see in HTML Form code above, there's settings_fields() and do_settings_sections(), those will return empty unless we register and add some fields for it. Take a look at code below, once we register fields it will be available to our admin HTML form above.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
//create adsense fields for admin page add_action( 'admin_init', 'wp_easy_adsense_register_filds' ); function wp_easy_adsense_register_filds() { //if you only want admin to modify adsense settings if ( !current_user_can( 'manage_options' ) ) { wp_die( __( 'No sufficient permissions.' ) ); } //register settings for our admin pages register_setting( 'wp-easy-adsense-option-group', 'wp_easy_adsense_code_top', 'wp_easy_adsense_input_sanitize'); register_setting( 'wp-easy-adsense-option-group', 'wp_easy_adsense_code_bottom', 'wp_easy_adsense_input_sanitize'); //add fields group section add_settings_section( 'section-one', __('Adsense Settings'), '', 'wp-easy-adsense' ); //add a field to admin pages add_settings_field( 'wp_easy_adsense_code_top', __('Enter Top Adsense Code'), 'wp_easy_adsense_code_field_top', 'wp-easy-adsense', 'section-one' ); add_settings_field( 'wp_easy_adsense_code_bottom', __('Enter Bottom Adsense Code'), 'wp_easy_adsense_code_field_bottom', 'wp-easy-adsense', 'section-one' ); } function wp_easy_adsense_code_field_top() { //text field $wp_easy_adsense_code_top = esc_attr( get_option( 'wp_easy_adsense_code_top' ) ); echo '<textarea name="wp_easy_adsense_code_top" rows="8" cols="50">'.$wp_easy_adsense_code_top.'</textarea>'; } function wp_easy_adsense_code_field_bottom() { //text field $wp_easy_adsense_code_bottom = esc_attr( get_option( 'wp_easy_adsense_code_bottom' ) ); echo '<textarea name="wp_easy_adsense_code_bottom" rows="8" cols="50">'.$wp_easy_adsense_code_bottom.'</textarea>'; } //sanitize field function function wp_easy_adsense_input_sanitize( $input ) { return htmlentities($input); }

Displaying Adsense Ads in Posts

Now we have everything ready, all we need to do is display Ads in our posts. This is super easy, just hook "wp_easy_adsenes_content" to a "the_content" filter action, and check if it's single page type using WordPress is_single() function, and then using get_option() get Adsense code which will go to both top and bottom of the posts.
PHP
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
add_filter( 'the_content', 'wp_easy_adsenes_content' ); //Add a filter to 'the_content' to hook ad_content() function function wp_easy_adsenes_content($content) { if (is_single()) //only apply to single posts, not homepage or category { $wp_easy_adsense_code_top = html_entity_decode( get_option( 'wp_easy_adsense_code_top' ) ); //get Adsense code $wp_easy_adsense_code_bottom = html_entity_decode( get_option( 'wp_easy_adsense_code_bottom' ) ); //get Adsense code $content = $wp_easy_adsense_code_top . $content . $wp_easy_adsense_code_bottom; //Attach code before and after content return $content; // return content with Adsense Code }else{ //for homepage, category or other pages, just return original content return $content; } }
That's it! We now have a complete Adsense Ad display plugin unless you want to add something more. I hope this tutorial has helped you understand WordPress plugin in basic level. I have included sample plugin file in download section, you can test it out in your WordPress site, last time plugin was successfully tested in WordPress version 3.9.1, all the best! Download
  • Even if “Insert Adsense” plugin hasn't been updated in more than 2 years, it works perfectly for me. In the last few days I wanted to change my main add on each page from a small one to a big one, and by only changing the HTML at Insert Adsense, all my pages changed at once. The other adds, stayed the same. It does exactly what it proposes…and works perfectly on my bilingual site. Here are examples of the same pages in two different languages: http://foradazonadeconforto.com/como-escolher-e-comprar-seguro-de-viagem/ (Brazilian Portuguese) http://outofyourcomfortzone.net/how-to-buy-travel-insurance/ (English) Simple and great plugin.
  • Note that you have to generate your adsense code from Google, and paste the entire code in the text boxes, replacing the existing text. There are three main text boxes corresponding to three ad locations - Lead-in, Mid-text and Lead-out. If you don't want to use a particular location, please suppress it by selecting the appropriate option. Otherwise, the plugin will show a red box indicating where you ad would be shown.
New question is currently disabled!