Skip to content

Instantly share code, notes, and snippets.

@donnamcmaster
Last active April 7, 2017 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donnamcmaster/51369db6fc96bf323112068a25026ae2 to your computer and use it in GitHub Desktop.
Save donnamcmaster/51369db6fc96bf323112068a25026ae2 to your computer and use it in GitHub Desktop.
WordPress: allows you to insert a widget into any content area using a shortcode
<?php
/**
* Widget Shortcode
* - allows you to insert a widget into any content area using a shortcode
* - credit: http://wp.smashingmagazine.com/2012/12/11/inserting-widgets-with-shortcodes/
* - read the comments on the Smashing Magazine article for pros and cons of this function
*/
add_shortcode( 'mcw_widget', function ( $atts ) {
// configure defaults and extract the attributes into variables
extract( shortcode_atts(
array(
'type' => '',
'title' => '',
'show_title' => false,
'count' => 0,
'image' => false,
),
$atts
));
$args = array(
'before_widget' => '<div class="box widget">',
'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