Stag Custom Sidebars Shortcode

Below, you’ll find a detailed guide on how to add the Stag Custom Sidebars Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Stag Custom Sidebars Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Stag Custom Sidebars Plugin and the shortcodes it provides:

Plugin Icon
Stag Custom Sidebars

"Stag Custom Sidebars is a versatile WordPress plugin that empowers users to create and manage unique, personalized sidebars on their website pages with ease and efficiency."

★★★★★ (7) Active Installs: 4000+ Tested with: 5.6.12 PHP Version: false
Included Shortcodes:
  • [stag_sidebar]

Stag Custom Sidebars [stag_sidebar] Shortcode

The Stag Sidebar shortcode allows you to create custom widget areas. It uses the ID and class attributes to customize the sidebar. The shortcode starts by checking if the sidebar is active and not in admin mode. It then uses ‘ob_start’ to turn on output buffering, ensuring the sidebar content is stored before being displayed. The ‘do_action’ function triggers custom actions before and after the sidebar. The ‘echo’ function outputs the HTML for the sidebar, with the ID and class attributes being escaped for security. The ‘dynamic_sidebar’ function displays the widgets for the specified sidebar ID. The output is then cleaned and returned.

Shortcode: [stag_sidebar]

Parameters

Here is a list of all possible stag_sidebar shortcode parameters and attributes:

  • id – The unique identifier for the specific sidebar.
  • class – The CSS class to style the sidebar.

Examples and Usage

Basic example – A simple usage of the stag_sidebar shortcode to display the sidebar with id=1.

[stag_sidebar id=1 /]

Advanced examples

Displaying a custom sidebar by referencing both ID and class. The sidebar will load by ID, and the class attribute will allow for custom styling of the sidebar.

[stag_sidebar id=1 class="my-custom-class" /]

Using the stag_sidebar shortcode to display multiple sidebars. This example shows how to display two different sidebars with id=1 and id=2, each with a different custom class for styling.

[stag_sidebar id=1 class="my-custom-class-1" /]
[stag_sidebar id=2 class="my-custom-class-2" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [stag_sidebar] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'stag_sidebar', array( &$this, 'stag_sidebar_shortcode' ) );

Shortcode PHP function:

                    function stag_sidebar_shortcode( $atts ) {
		$atts = shortcode_atts(
			array(
				'id'    => '1',
				'class' => '',
			),
			$atts
		);

		$output = '';

		if ( is_active_sidebar( $atts['id'] ) && ! is_admin() ) {
			ob_start();

			do_action( 'stag_custom_sidebars_before', $atts['id'] );

			echo "<section id='" . esc_attr( $atts['id'] ) . "' class='stag-custom-widget-area " . esc_attr( $atts['class'] ) . "'>";
			dynamic_sidebar( $atts['id'] );
			echo '</section>';

			do_action( 'stag_custom_sidebars_after' );

			$output = ob_get_clean();
		}

		return $output;
	}
                    

Code file location:

stag-custom-sidebars/stag-custom-sidebars/stag-custom-sidebars.php

Conclusion

Now that you’ve learned how to embed the Stag Custom Sidebars Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *