- Wordpress -
[2006-04-10]
Coding Widgets is very simple. I already showed how to Widgetize third party plugin by simply inserting code into functions.php file. Here I decided to present an example of Adsense Widget coding. This time the Widget will be standalone.
Below is full Adsense Widget code. All you need to do is to put inside Adsense code.
<?php
/*
Plugin Name: adsense_widget
Description: Adsense widget.
Author: Dude.
Version: 1.0
Author URI: http://www.example.com
*/
function widget_adsense_init() {
// Check for the required API functions
if ( !function_exists('register_sidebar_widget') ||
!function_exists('register_widget_control') )
return;
function widget_adsense($args) {
extract($args);
?>
<li>
// Google Adsense Code Goes Here
</li>
<?php
}
register_sidebar_widget('adsense','widget_adsense');
}
// Delay plugin execution to ensure Dynamic Sidebar
// has a chance to load first
add_action('plugins_loaded', 'widget_adsense_init');
?>
Copy and save this code into php file and put it in widgets folder (you can place it into plugins folder as well). Put inside adsense code and you get adsense widget.
If you are using more than one adsense on your sidebar, then just make copy of this widget and change name to something like: Adsense_BlaBla. Do not forget to change all functions names, so that Widget functions are unique.
I wrote an AdSense widget yesterday too. Only I didn’t want to tie it down to specific AdSense code, so like the text widget, mine is configurable. You can add up to 4 AdSense widget entries and paste the AdSense code Google gives you into the config screen for each one.
Check it out here: http://ottodestruct.com/blog/2006/04/09/fun-with-widgets/
Nice work!