add_action('widgets_init', 'create_arbitrary_sidebar');
function create_arbitrary_sidebar(){
register_sidebar(array(
'name' => __('Arbitrary Widgets'),
'id' => 'arbitrary',
'description' => sprintf(__('Widgets from this area can added into posts/pages using the %1$s or %2$s shortcodes.'), '[widget ID]', '[widget Name]'),
'before_widget' => '<div class="block"><div class="block-content block-%2$s clear-block" id="instance-%1$s">',
'after_widget' => '</div></div>',
'before_title' => '<h3 class="title">',
'after_title' => '</h3>'
));
}
add_action('in_widget_form', 'widget_shortcodes_info', 10, 3);
function widget_shortcodes_info($widget, $return, $instance){
if(!is_numeric($widget->number)) return; // wp-save bug :( widget needs to be saved first...
global $wp_registered_widgets;
// get the active widgets from all sidebars
$sidebars_widgets = wp_get_sidebars_widgets();
// prepare matches
$matches = array();
foreach($wp_registered_widgets as $i => $w)
if($w['name'] == $widget->name) $matches[] = $w['id'];
// find out the widget position (number)
$number = 0;
$is_arbitrary = false;
if(!empty($sidebars_widgets['arbitrary']))
foreach($sidebars_widgets['arbitrary'] as $i => $value):
if(in_array($value, $matches) && !$is_arbitrary) $number = $number +1;
if($value == $widget->id) $is_arbitrary = true;
endforeach;
echo '<div style="background:#eee; padding: 5px;">To include this widget into your posts or pages use one of the following shortcodes: <br />';
echo '<code>[widget '.substr(md5($widget->id), 0, 8).']</code> <br /> <code>[widget "'.$widget->name.'"'.(($number > 1) ? ' number='.$number : null).']</code></div>';
}
add_shortcode('widget', 'shortcode_widget');
function shortcode_widget($atts){
global $wp_registered_widgets, $wp_registered_sidebars;
extract(shortcode_atts(array(
'number' => false, // only taken in consideration if the 1st argument is the "Widget Name" (not the hashed ID)
'title' => true, // show titles?
'area' => 'arbitrary' // sidebar to search
), $atts));
// get 1st parameter (assuming this is the target widget id or name)
if (!empty($atts[0])) $widget = esc_attr($atts[0]); else return;
$sidebar = esc_attr($area);
$number = intval($number);
$callback = false;
$possible_matches = array();
$sidebars_widgets = wp_get_sidebars_widgets();
if((empty($sidebars_widgets[$sidebar]) || empty($wp_registered_widgets)) && (current_user_can('edit_themes')))
return "no valid active widgets in {$sidebar}";
// assuming we get the md5 hashed ID
foreach ($wp_registered_widgets as $i => $w)
if ($widget == substr(md5($w['id']), 0, 8)):
$callback = ($w['callback']);
$widget = $w['id']; // real widget ID
// compare widget names as well, and build a array with the possible widget matches array
// (which is used later if ID match fails)
elseif($widget == $w['name']):
$possible_matches[] = $w['id'];
endif;
// nothing found, assume it's the "Widget Name".
if(!$callback):
$valid_matches = array();
foreach($sidebars_widgets[$sidebar] as $i => $w)
foreach($possible_matches as $id) if($id == $w) $valid_matches[] = $w;
if(!empty($valid_matches)) $widget = $number ? $valid_matches[$number-1] : $widget = $valid_matches[0];
if($widget && isset($wp_registered_widgets[$widget]['callback'])) $callback = $wp_registered_widgets[$widget]['callback'];
endif;
// yey. we found it
if($callback):
ob_start();
$params = array_merge(array(array_merge($wp_registered_sidebars[$sidebar], array('widget_id' => $widget, 'widget_name' => $wp_registered_widgets[$widget]['name']))), (array)$wp_registered_widgets[$widget]['params']);
$classname_ = '';
foreach ((array)$wp_registered_widgets[$widget]['classname'] as $cn)
if (is_string($cn)) $classname_ .= '_'.$cn; elseif (is_object($cn)) $classname_ .= '_'.get_class($cn);
$classname_ = ltrim($classname_, '_');
$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $widget, $classname_);
$params = apply_filters('dynamic_sidebar_params', $params);
if (is_callable($callback)) call_user_func_array($callback, $params);
$output = ob_get_clean();
// remove h3 if title = false
if(!$title) $output = preg_replace('#<h3 class="title">(.*?)</h3>#', '', $output);
return $output;
else:
return "widget instance not found: ".esc_attr($atts[0]);
endif;
}