AJ Clarke

One of the most annoying issues when dealing with shortcodes in WordPress is that if the content inside the shortcodes has any spacing at the top or bottom the post editor will adding some blank p and/or br tags into the code. This can have a negative affect on the rending of the shortcode, specifically when your theme is applying any sort of styling to the p tags, such as margins. To prevent this you can go in HTML mode and make sure there isn’t any spacing and so forth, however, this can be a real hassle. Below is a small function which will strip away any open p tags before a bracket and closing tags after the bracket as well as any break tags after a closing bracket in the post editor (the brackets are most commonly used for shortcodes – that’s why we target them specifically).