Search code examples
phphtmlwordpressshortcode

Wordpress keeps adding auto paragraph tags to shortcodes and content


I can't seem to stop wordpress from auto adding paragraphs to every line I type, including shortcodes and any raw HTML I enter into the visual composer. I have tried the plugins "Toggle wpautop" and "Raw html" to try to convert it, however it never works. Is it because I'm using visual composer? It just wraps p tags around literally anything.

enter image description here


Solution

  • The problem is not with Visual Composer, it happens purely because of the autop filter on the_content. There are a few ways to tackle it, but IMHO a content filter is the best way to deal with it.

    If you're comfortable editing your functions.php you can filter the the_content hook to remove <p> tags surrounding your shortcodes with strtr by adding the following:

    add_filter('the_content', 'remove_unneeded_silly_p_tags_from_shortcodes');
    function remove_unneeded_silly_p_tags_from_shortcodes($the_content){
        $array = array (
            '<p>['      => '[', //replace "<p>[" with "["
            ']</p>'     => ']', //replace "]</p>" with "]"
            ']<br />'   => ']' //replace "]<br />" with "]"
        );
        $the_content = strtr($the_content, $array); //replaces instances of the keys in the array with their values
        return $the_content;
    }
    

    The other alternatives (like removing autop from the_content) tend to have pretty far reaching consequences, so I tend to avoid that. You could also try to remove margin stylings from that specific paragraph tag that gets added, but because of the auto-adding it may be difficult to target that particular tag...