the_widget : the way 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
       if(function_exists('the_widget')) { // If the_widget is supported
           // Widget code here
       }
    ?>
  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:
     class shailan_DropdownWidget extends WP_Widget { 
  4. We can create the_widget code with default arguments like this:
    <?php the_widget('shailan_DropdownWidget'); ?>

    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.
    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(function_exists('the_widget')) { // If the_widget is supported
            the_widget('shailan_DropdownWidget', 'type=Pages&amp;Exclude=2,4');
      }
    ?>

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

Related Posts

NS Weight Release

WP-Backgrounds Lite by InoPlugs Web Design and Juwelier Schönmann 1010 Wien