• I know this has been discussed to death in previous threads, but most of them are now closed. I’ve got a couple of outside the box fixes for this.

    1) use your theme code to override the wp_autop filter, I shut it off for any content that begins with an HTML5 <section>:

    function no_autop($pee) {
    	$text = trim($pee);
            $len = strpos($text, ' ');
            $text = substr($text, 0, $len);
    	if ('<section' == $text) {
    		return $pee;
    	}
    	else {
    		return wpautop($pee);
    	}
    }
    
    // disable auto-p
    remove_filter('the_content', 'wpautop');
    
    // add conditional auto-p
    add_filter('the_content', 'no_autop');

    I suppress the added <p></p> and
    tags in some cases using jQuery and a line of css:

    jQuery(function()
    {
         jQuery("p:empty").addClass('empty');
         jQuery("img").prev("br").addClass('empty');
         jQuery("section").prev("br").addClass('empty');
    });
    .empty {
        display:none;
    }

    You could also just remove them in the jQuery or with a regex after the call to wp_autop in the no_autop function, it’s up to you.

    /peter

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Peter Wooster

    (@pkwooster)

    Another item to add to this, the Don’t Break the Code plugin helps a lot by allowing you to disable the visual editor on specific posts or pages.

    Thread Starter Peter Wooster

    (@pkwooster)

    More results on this:

    Using javascript causes the page to jump on load since there is an unwanted empty paragraph or a br at the top. So I decided that the regex solution was needed instead, here’s a crude version of an auto wrapper.

    function new_autop($pee) {
        	$text = substr(trim($pee), 0, 8);
            if('[noautop' !=  $text && '<section' != $text ){
    	    $pee = preg_replace('/<(.*?)>/se', '"<".str_replace("\n", " ", "$1").">"', $pee);
                $pee2 = wpautop($pee);
                $pee2 = preg_replace('/(.*)<p>\s*<\/p>/s', '$1', $pee);
                $pee2 = preg_replace('/(.*)<br \/>\s*</s', '$1 <', $pee);
            }
            return $pee;
    }

    If you’re good with regex, please show me how to clean this up, but it appears to work. The strategy is:
    – return content as is if it starts with <section or [noautop
    – remove newlines from inside html tags
    – call wpautop
    – remove empty paragraphs
    – remove br before html tags

    Thread Starter Peter Wooster

    (@pkwooster)

    This works much better after you fix the really obvious bug. I’ll leave that as an exercise for the reader.

    How can i always show the <p> tag when i change to “html” in the editor??

    Thread Starter Peter Wooster

    (@pkwooster)

    The <p> tags aren’t even there. That’s the problem, they are added by wp-autop when the page or post is displayed. They appear in your code as double newlines.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘wp_autop – Leave my HTML Alone’ is closed to new replies.