Why does WP not like my container?

I think it’s safe to say that wpautop() is a basket case without hurting anyone’s feelings, but I wouldn’t remove & add it at a different priority as that just makes things worse, as demonstrated (although what you posted is the browser trying to make sense of broken html, rather than the actual output, which does only have the <a> tag once).

To fix up the mangled html produced at normal priority I’ve found applying this code to the shortcode content fixes up most problems:

function cleanup_sc_content( $sc_content ) {
    $sc_content = force_balance_tags( $sc_content );
    $sc_content = preg_replace( '/<p>\s*+(<br\s*\/*>)?\s*<\/p>/i', '', $sc_content );
    return $sc_content;
}

which is a repurposing (with trivial modifications) of the original remove_empty_p() function posted by @Michelle, which in turn is due to @mtinsley (github ninnypants, original gist ).

To use it, I wouldn’t personally add it as a filter but apply it directly to the shortcode content, eg:

return '<a' . $link . '>' . $before  
   . cleanup_sc_content( do_shortcode( shortcode_unautop( $content ) ) ) 
   . $after . '</a>';

(note the shortcode_unautop() hack as well is needed, as although wpautop() runs on the content of shortcodes, shortcode_unautop() doesn’t – yes, really). However given the constant whack-a-mole “fixes” of wpautop() and the many outstanding and confusing bugs (spanning years) that may or may not apply, this (and any) solution will be unstable.

Leave a Comment