WPAssist

WordPress Hacks · WordPress Tips · Published December 2009

the_widget : How to Use a Widget Whereever You Like

Since 2.8, WordPress allows us to use widgets in our templates. This made creating websites easier as well as creating professional themes. Now we can use any widget in our headers, footers or our page templates even if we don’t have a sidebar there.

Here is how to embed a widget in our template:

  1. Open the file we want to embed widget. (Eg. “header.php” or “page.php”)
  2. Paste this code whereever you like!

```php

``` 3. Now we have to find widget classname for the widget we want to embed. You can ask the developer if you are not comfortable with php. Or you can manually search for the widget class name. Here is an example of what it looks like:

php class shailan_DropdownWidget extends WP_Widget { 4. We can create the_widget code with default arguments like this:

```php

```

But that's not what we intend for. We want more control! 5. Now lets find supported widget arguments. Usually this could be documented by the author, if not, we can find the arguments in the widget function.

php function widget($args, $instance) { extract( $args ); $title = apply_filters('widget_title', $instance['title']); $type = $instance['type']; $exclude = $instance['exclude']; ... 6. Here is the final result with custom arguments:

```php

```

If you want to know how to add WordPress RSS widget using php, continue reading here.