donnamcmaster
4/7/2017 - 9:21 PM

WordPress: allows you to insert a widget into any content area using a shortcode

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;
});