It is really easy to write a widget to the wordpress blog. So I thought of writing my own widget to show Google ads in my Blog. Here is how I did it.
- First generate the JavaScript code for your adsense account from the Google Adsense Page. You can do this by sigining in to the Dashboard of Adsense from https://www.google.com/adsense/. Then click the “Adsense Setup” tab and follow the wizard.
- Inside the “wp-content/plugins” directory of your WordPress installation create a file for your plugin. (Say myadsense_widget.php)
- Then first write the code that should be appeared in your widget. In this case you can just echo the the code provide by the Google. Anyway In order to make your widget complaint with the current theme, you have to use the code similar to the following.
// the function for the widget function widget_myadsense($args) { // being aware of the theme extract($args); echo $before_widget; echo $before_title . "Google Adsense". $after_title; // here you just echo the code provided by the google echo <<<GOOGLE_JS <!-- in this space you have to copy paste the code provided by the google--> GOOGLE_JS; // again being aware of the theme echo $after_widget; }
- Then write the code to register the above function as a widget with the following piece of code.
// initiating widget function myadsense_init() { register_sidebar_widget(__('My Adsense'), 'widget_myadsense'); } // adding the action add_action("plugins_loaded", "myadsense_init");
- We are almost done here, But don’t forget you can mention your information as the widget plugin author with a comment similar to the following template.
/* Plugin Name: MyAdsense Plugin URI: http://dimuthu.org Description: Adsense Plugin for my blog Author: Dimuthu Gamage Version: 0.1 Author URI: http://dimuthu.org */
- That is all you have to code. Now just go the Plugins section of the wordpress from your Dashboard and enable the plugin (“myadsesne”) you just created.
- Go to the Design->Widget section and add your Widget to the Sidebar and click “Save Changes”. And go to your blog URL and make sure that the Ads are shown in there.