Widget Area Shortcode
Adds a [widget_area] shortcode that prints any widget area inside a page, post or block template, so the same widgets can be reused anywhere.
Opens WPIDE on
Code
add_shortcode( 'widget_area', function ( $atts ) {
static $depth = 0;
$atts = shortcode_atts( array( 'id' => '' ), $atts, 'widget_area' );
$id = sanitize_text_field( $atts['id'] );
if ( '' === $id || $depth > 0 || ! is_active_sidebar( $id ) ) {
return '';
}
$depth++;
ob_start();
dynamic_sidebar( $id );
$html = ob_get_clean();
$depth--;
return '<div class="wpide-widget-area wpide-widget-area-' . esc_attr( sanitize_html_class( $id ) ) . '">' . $html . '</div>';
} );
Notes
Example: [widget_area id="sidebar-1"]. Find the IDs of your widget areas in your theme or with the Register Extra Widget Areas snippet.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.