Skip to content

Instantly share code, notes, and snippets.

@sheabunge
Created December 12, 2012 10:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheabunge/4266741 to your computer and use it in GitHub Desktop.
Save sheabunge/4266741 to your computer and use it in GitHub Desktop.
Easily insert any WordPress sidebar widget into a post or page using a shortcode. http://wp.smashingmagazine.com/2012/12/11/inserting-widgets-with-shortcodes/
<?php
/**
* Plugin Name: Widget Shortcode
* Plugin URI: http://wp.smashingmagazine.com/2012/12/11/inserting-widgets-with-shortcodes/
* Description: Easily insert any WordPress sidebar widget into a post or page using a shortcode.
* Author: Smashing Magazine
* Author URI: http://www.smashingmagazine.com
* Version: 1.0
*/
add_shortcode( 'widget', 'my_widget_shortcode' );
function my_widget_shortcode( $atts ) {
// Configure defaults and extract the attributes into variables
extract( shortcode_atts(
array(
'type' => '',
'title' => '',
'scheme' => 'light'
),
$atts
));
$args = array(
'before_widget' => '<div class="box widget scheme-' . $scheme . ' ">',
'after_widget' => '</div>',
'before_title' => '<div class="widget-title">',
'after_title' => '</div>',
);
ob_start();
the_widget( $type, $atts, $args );
$output = ob_get_clean();
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment