# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Widgets](https://wpide.com/library/category/widgets/)
- Tags: widgets, shortcode, sidebar
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/widget-area-shortcode/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Widget Area Shortcode" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
