X

Add div element shortcode including class

This snippet creates a simple shortcode that will let you add a div by adding the shortcode to your posts and pages. Add this snippet to the functions.php file of your WordPress theme. Within any post, you can write [div class="my_class"]content[/div] to include a <div class="my_class"></div> element within your post. This shortcode comes in handy if you need to do something custom on a post by post basis.

function div_html($atts, $content = null) {
	
	extract( shortcode_atts( array(
		'class' => '',
	), $atts ) );

	$class = $class ? " class=\"$class\"" : NULL;
	
	return "<div$class>$content</div>";
}
add_shortcode('div', 'div_html');

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!