Advertisement
alchymyth

widget shortcode untested

Sep 19th, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. // Adding widgets with a shortcode anywhere in a page as per Digging Into WordPress
  2.  
  3. function widget($atts) {
  4.  
  5. global $wp_widget_factory;
  6.  
  7. extract(shortcode_atts(array(
  8. 'widget_name' => FALSE,
  9. 'title' => __('Recent Posts'),
  10. 'number' => 10,
  11. ), $atts));
  12.  
  13. $widget_name = wp_specialchars($widget_name);
  14.  
  15. if (!is_a($wp_widget_factory->widgets[$widget_name], 'WP_Widget')):
  16. $wp_class = 'WP_Widget_'.ucwords(strtolower($class));
  17.  
  18. if (!is_a($wp_widget_factory->widgets[$wp_class], 'WP_Widget')):
  19. return '<p>'.sprintf(__("%s: Widget class not found. Make sure this widget exists and the class name is correct"),'<strong>'.$class.'</strong>').'</p>';
  20. else:
  21. $class = $wp_class;
  22. endif;
  23. endif;
  24.  
  25. if( $widget_name == 'WP_Widget_Recent_Posts' ) $instance = 'title='.$title.'&number='.$number;
  26.  
  27. ob_start();
  28. the_widget($widget_name, $instance, array('widget_id'=>'arbitrary-instance-'.$id,
  29. 'before_widget' => '',
  30. 'after_widget' => '',
  31. 'before_title' => '',
  32. 'after_title' => ''
  33. ));
  34. $output = ob_get_contents();
  35. ob_end_clean();
  36. return $output;
  37.  
  38. }
  39. add_shortcode('widget','widget');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement