WordPress force_balance_tags function and short tag fix

I was buidling something with WordPress shorttag feature, I manage to get it working. However this shorttag add extra </p> and it's simply too hard to remove it! And I discovered this - force_balance_tags function.

This is the shortcode:

function column_shortcode( $atts, $content = null ) {
   return '<div class="column">' . $content . '</div>';
add_shortcode( 'col', 'column_shortcode' );

And the HTML result looks like this

<div class="column"></p>
	<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

See that extra </p>? It shouldn't be there. Not sure if this the correct way, but force_balance_tags function fixes it:

function column_shortcode( $atts, $content = null ) {
   return '<div class="column">' . force_balance_tags($content) . '</div>';
add_shortcode( 'col', 'column_shortcode' );

Share the love

Leave a Comment

Please keep in mind that comments are moderated and rel="nofollow" is in use. You can use [code][/code] if you want to write codes. Don't spam us :) Thanks!