Adding new widgets in wordpress themes

We can add new widgets by using functions.php in the theme folder.

For example I can add “Services I offer and Google Adsense in 2 ways
to the sidebars.

1. Code both to the page sidebar.php
3. Create widgets using functions.php file in the theme folder

Following code shows how I added a simple Adsense widget using functions.php

if (function_exists(‘register_sidebar’))
register_sidebar_widget(__(‘Adsense’), ‘google_widget’);

function google_widget() {
echo $before_widget;
echo $before_title;

echo $after_title; 

Here comes Adsense code

 <?php echo $after_widget; 

function init_adsense(){
    register_adsense_widget(“Sponsors”, “google_widget”);    

add_action(“plugins_loaded”, “init_adsense”);

The above code gives a simple widget named adsense in the widget panel. From the widget list I can add it to the sidebar without disturbing sidebar.php file.


Just now, I  noticed another method,  if the widget contains text or HTML only. In such cases you can use text widgets. Add the already existing “Text widget”.

In the example above give the title as “Sponsors” and write adsense code in the box. It is the easiest way if the widget you are making contains only text or HTML.

Leave a Reply