Back WordPress

WordPress force_balance_tags function and short tag fix

WRITTEN BY ON 15 May 2014
8,379 VIEWS • SHARES
0 comments

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>
</div>	

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' );
Join the discussion

Comments will be moderated and rel="nofollow" will be added to all links. You can wrap your coding with [code][/code] to make use of built-in syntax highlighter.