function do_widget($atts)
{
    global $wp_registered_widgets, $_wp_sidebars_widgets, $wp_registered_sidebars;
    /* check if the widget is in  the shortcode x sidebar  if not , just use generic, 
    if it is in, then get the instance  data and use that */
    if (isset($_wp_sidebars_widgets)) {
        amr_show_widget_debug('which one');
        //check for debug prompt and show widgets in shortcode sidebar if requested and logged in etc
    } else {
        echo '<br />No widgets defined at all in any sidebar!';
        return false;
    }
    extract(shortcode_atts(array('sidebar' => 'Widgets for Shortcodes', 'id' => '', 'name' => '', 'title' => '', 'class' => 'amr_widget', 'wrap' => '', 'widget_classes' => ''), $atts));
    /* compatibility check - if the name is not entered, then the first parameter is the name */
    if (empty($name) and !empty($atts[0])) {
        $name = $atts[0];
    }
    /* the widget need not be specified, [do_widget widgetname] is adequate */
    if (!empty($name)) {
        // we have a name
        $widget = $name;
        foreach ($wp_registered_widgets as $i => $w) {
            /* get the official internal name or id that the widget was registered with  */
            if (strtolower($w['name']) === strtolower($widget)) {
                $widget_ids[] = $i;
            }
            //if ($debug) {echo '<br /> Check: '.$w['name'];}
        }
    } else {
        /* check for id if we do not have a name */
        if (!empty($id)) {
            /* if a specific id has been specified */
            foreach ($wp_registered_widgets as $i => $w) {
                /* get the official internal name or id that the widget was registered with  */
                if ($w['id'] === $id) {
                    $widget_ids[] = $id;
                }
            }
            //if ($debug) { echo '<h2>We have an id: '.$id.'</h2>'; 	if (!empty($widget_ids)) var_dump($widget_ids);	}
        } else {
            echo '<br />No valid widget name or id given in shortcode parameters';
            return false;
        }
    }
    if (empty($widget)) {
        $widget = '';
    }
    if (empty($id)) {
        $id = '';
    }
    if (empty($widget_ids)) {
        echo '<br /><a href="" title="Error: Your Requested widget ' . $widget . ' ' . $id . ' is not in the widget list. Typo maybe?">!</a><br />';
        amr_show_widget_debug('empty', $atts);
        return false;
    }
    if (!($sidebarid = get_sidebar_id($sidebar))) {
        $sidebarid = $sidebar;
    }
    /* get the official sidebar id for this widget area - will take the first one */
    if (empty($widget)) {
        $widget = '';
    }
    $content = '';
    /* if the widget is in our chosen sidebar, then use the options stored for that */
    if (!isset($_wp_sidebars_widgets[$sidebarid]) or empty($_wp_sidebars_widgets[$sidebarid])) {
        // try upgrade
        amr_upgrade_sidebar();
    }
    if (isset($_wp_sidebars_widgets[$sidebarid]) and !empty($_wp_sidebars_widgets[$sidebarid])) {
        /*		if ($debug) { 
        			echo '<br />Widget ids in sidebar: "'.$sidebar.'" with id: '.$sidebarid .'<br />';
        			sort ($_wp_sidebars_widgets[$sidebarid]);
        			foreach ($_wp_sidebars_widgets[$sidebarid] as $i=> $w) {
        				echo $i.' '.$w.'<br />';
        			};	
        		}
        		*/
        /* get the intersect of the 2 widget setups so we just get the widget we want  */
        $wid = array_intersect($_wp_sidebars_widgets[$sidebarid], $widget_ids);
        /*			if ($debug) { echo '<br />Will use widget ids'.'<br />';
        				foreach ($widget_ids as $i=> $w) {
        					echo '&nbsp;&nbsp;&nbsp;'.$w.'<br />';
        				};
        			}
        */
    } else {
        /* the sidebar is not defined */
        //if ($debug) {
        echo '<br /><a href="" title="Error: Sidebar ' . $sidebar . ' with sidebarid ' . $sidebarid . ' is empty (no widgets) or is not defined.">!</a><br />';
        //}
    }
    $output = '';
    if (empty($wid) or !is_array($wid) or count($wid) < 1) {
        //if ($debug) {
        echo '<br /><a href="" title="Error: Your requested Widget ' . $widget . ' is not in the ' . $sidebar . ' sidebar ">!</a><br />';
        amr_show_widget_debug('empty', $atts);
        //}
        unset($sidebar);
        unset($sidebarid);
    } else {
        /*  There may only be one but if we have two in our chosen widget then it will do both */
        $output = '';
        foreach ($wid as $i => $widget_instance) {
            ob_start();
            /* catch the echo output, so we can control where it appears in the text  */
            shortcode_sidebar($widget_instance, $sidebar, $title, $class, $wrap, $widget_classes);
            $output .= ob_get_clean();
        }
    }
    return $output;
}
function amr_do_widget($atts)
{
    global $wp_registered_widgets, $_wp_sidebars_widgets, $wp_registered_sidebars;
    /* check if the widget is in  the shortcode x sidebar  if not , just use generic, 
    if it is in, then get the instance  data and use that */
    if (is_admin()) {
        return '';
    }
    // eg in case someone decides to apply content filters when apost is saved, and not all widget stuff is there.
    extract(shortcode_atts(array('sidebar' => 'Widgets for Shortcodes', 'id' => '', 'name' => '', 'title' => '', 'class' => 'amr_widget', 'wrap' => '', 'widget_classes' => ''), $atts));
    if (isset($_wp_sidebars_widgets)) {
        amr_show_widget_debug('which one', $name, $id, $sidebar);
        //check for debug prompt and show widgets in shortcode sidebar if requested and logged in etc
    } else {
        $output = '<br />No widgets defined at all in any sidebar!';
        return $output;
    }
    /* compatibility check - if the name is not entered, then the first parameter is the name */
    if (empty($name) and !empty($atts[0])) {
        $name = $atts[0];
    }
    /* the widget need not be specified, [do_widget widgetname] is adequate */
    if (!empty($name)) {
        // we have a name
        $widget = $name;
        foreach ($wp_registered_widgets as $i => $w) {
            /* get the official internal name or id that the widget was registered with  */
            if (strtolower($w['name']) === strtolower($widget)) {
                $widget_ids[] = $i;
            }
            //if ($debug) {echo '<br /> Check: '.$w['name'];}
        }
        if (!($sidebarid = amr_get_sidebar_id($sidebar))) {
            $sidebarid = $sidebar;
            /* get the official sidebar id for this widget area - will take the first one */
        }
    } else {
        /* check for id if we do not have a name */
        if (!empty($id)) {
            /* if a specific id has been specified */
            foreach ($wp_registered_widgets as $i => $w) {
                /* get the official internal name or id that the widget was registered with  */
                if ($w['id'] === $id) {
                    $widget_ids[] = $id;
                }
            }
            //echo '<h2>We have an id: '.$id.'</h2>'; 	if (!empty($widget_ids)) var_dump($widget_ids);
        } else {
            $output = '<br />No valid widget name or id given in shortcode parameters';
            return $output;
        }
        // if we have id, get the sidebar for it
        $sidebarid = amr_get_widgets_sidebar($id);
        if (!$sidebarid) {
            $output = '<br />Widget not in any sidebars<br />';
            return $output;
        }
    }
    if (empty($widget)) {
        $widget = '';
    }
    if (empty($id)) {
        $id = '';
    }
    if (empty($widget_ids)) {
        $output = '<br />Error: Your Requested widget "' . $widget . ' ' . $id . '" is not in the widget list.<br />';
        $output .= amr_show_widget_debug('empty', $name, $id, $sidebar);
        return $output;
    }
    if (empty($widget)) {
        $widget = '';
    }
    $content = '';
    /* if the widget is in our chosen sidebar, then use the options stored for that */
    if (!isset($_wp_sidebars_widgets[$sidebarid]) or empty($_wp_sidebars_widgets[$sidebarid])) {
        // try upgrade
        amr_upgrade_sidebar();
    }
    //if we have a specific sidebar selected, use that
    if (isset($_wp_sidebars_widgets[$sidebarid]) and !empty($_wp_sidebars_widgets[$sidebarid])) {
        /* get the intersect of the 2 widget setups so we just get the widget we want  */
        $wid = array_intersect($_wp_sidebars_widgets[$sidebarid], $widget_ids);
    } else {
        /* the sidebar is not defined or selected - should not happen */
        if (isset($debug)) {
            // only do this in debug mode
            if (!isset($_wp_sidebars_widgets[$sidebarid])) {
                $output = '<p>Error: Sidebar "' . $sidebar . '" with sidebarid "' . $sidebarid . '" is not defined.</p>';
            } else {
                $output = '<p>Error: Sidebar "' . $sidebar . '" with sidebarid "' . $sidebarid . '" is empty (no widgets)</p>';
            }
        }
    }
    $output = '';
    if (empty($wid) or !is_array($wid) or count($wid) < 1) {
        $output = '<p>Error: Your requested Widget "' . $widget . '" is not in the "' . $sidebar . '" sidebar</p>';
        $output .= amr_show_widget_debug('empty', $name, $id, $sidebar);
        unset($sidebar);
        unset($sidebarid);
    } else {
        /*  There may only be one but if we have two in our chosen widget then it will do both */
        $output = '';
        foreach ($wid as $i => $widget_instance) {
            ob_start();
            /* catch the echo output, so we can control where it appears in the text  */
            amr_shortcode_sidebar($widget_instance, $sidebar, $title, $class, $wrap, $widget_classes);
            $output .= ob_get_clean();
        }
    }
    return $output;
}