WordPress.org

Ready to get started?Download WordPress

Forums

wp_autop - Leave my HTML Alone (6 posts)

  1. Peter Wooster
    Member
    Posted 2 years ago #

    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

  2. Peter Wooster
    Member
    Posted 2 years ago #

    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.

  3. Peter Wooster
    Member
    Posted 2 years ago #

    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

  4. Peter Wooster
    Member
    Posted 2 years ago #

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

  5. Diogo15
    Member
    Posted 2 years ago #

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

  6. Peter Wooster
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic