Add widgets from shortcode in post/page

WordPress Widgets can be easily customized and used in your theme using dynamic_sidebar(‘WIDGET NAME’) function. But when the time comes to use it in page or post it is not that easy. As the page and post only accepts shortcodes,  you need to write a shortcode which will get contains from widgets. Below you can see steps to call widgets from shortcode.

1. Register A Sidebar


register_sidebar(array(
'name'			=> 'My Sidebar',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));

2. Create a function to read sidebar


function get_my_sidebar($output) {
ob_start();
dynamic_sidebar('My Sidebar');
$output = ob_get_contents();
ob_end_clean();
return $output;

3. Create Shortcode


add_shortcode('sidebar_shortcode','get_my_sidebar');


4. Use it
Use


[sidebar_shortcode]

in post/page to get the widgets assigned in sidebar.

Note
Use these codes in functions.php of your active theme.