Using Enclosing Shortcode

In this short snippet we’re going to make use of enclosing shortcode. It’s very useful if you want to use a div with specific class multiple times.

function div_enclose_callback($atts = [],$content = null){
	$a = shortcode_atts( array(
		'class' => '',
	), $atts );

	<div class="<?php echo $a['class'] ?>" ><?php echo $content; ?></div>

	return ob_get_clean();
add_shortcode( 'div_enclose', 'div_enclose_callback' );

The usage would be this.

[div_enclose class='custom']Test[/div_enclose]
<!--expected output:-->
<div class='custom'>Test</div>

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.