Advertisement
daymobrew

Widget Area Shortcode

Dec 28th, 2020
1,306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Plugin Name: Widget Area Shortcode
  5. Description: A shortcode to display a widget area.
  6. Plugin URI: https://www.damiencarbery.com
  7. Version: 0.1
  8. Author: Damien Carbery
  9. */
  10.  
  11. defined( 'ABSPATH' ) || exit;
  12.  
  13. add_action( 'widgets_init', 'dcwd_register_sidebar_for_shortcode' );
  14. function dcwd_register_sidebar_for_shortcode() {
  15.     $args = array(
  16.         'name'          => 'Shortcode',
  17.         'id'            => 'sidebar-shortcode',
  18.         'description'   => 'Sidebar that will be displayed via a shortcode.',
  19.         'class'         => 'sidebar-shortcode',
  20.         'before_widget' => '<div id="%1$s" class="widget %2$s">',
  21.         'after_widget'  => '</div>',
  22.         'before_title'  => '<h2 class="widgettitle">',
  23.         'after_title'   => '</h2>'
  24.     );
  25.    
  26.     register_sidebar( $args );
  27. }
  28.  
  29.  
  30. add_shortcode( 'sidebar_display', 'dcwd_sidebar_display' );
  31. function dcwd_sidebar_display() {
  32.     if ( is_active_sidebar( 'sidebar-shortcode' ) ) {
  33.         ob_start();
  34.         dynamic_sidebar( 'sidebar-shortcode' );
  35.         return ob_get_clean();
  36.     }
  37.    
  38.     return '<p>sidebar not active.</p>';
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement