• H2GD Part 28: More battles with wpautop() generating invalid HTML

    When I was developing my oik-css plugin recently I once again suffered from strange results due to wpautop() filter function processing of the “the_content” filter. Since the wpautop() filter function normally runs BEFORE shortcode expansion I was getting unwanted <p>’s, </p>’s, and <br />’s generated in the CSS content. …So my internal CSS styling was not working.

    I found a solution that had been discussed on stack overflow and implemented that as bw_better_autop().

    Knowing how much trouble people have had with wpautop() processing I was concerned that there would be some side effects. I wasn’t that surprised when the solution failed.

    Problem with add_to_cart shortcode

    I was developing a site using woocommerce and oik-css and noticed that the formatting for the [add_to_cart] shortcode was wrong; the grey border did not surround the price and Add to cart button.

    I tracked this down to the fact that wpautop() was now running after do_shortcodes() and was adding an ending </p> where it was not needed – in the middle of the valid HTML that add_to_cart had generated.

    Simpler version of the problem

    The problem can be demonstrated with some simple HTML.

    8.99

    All I’ve done here is to insert blank lines between the opening p and the closing p. If you inspect the generated HTML you’ll see that unwanted tags that have been added. This is the GeSHi formatted version of the HTML that I started with.

    <p class="product" style="border: 2px solid gray; padding: 12px;">

    8.99

    </p>

    Workaround

    The fact that I’m using oik-css processing on this site is not the issue. As demonstrated above the problem occurs during wpautop() filter function processing. Clearly there’s still a problem with wpautop(), regardless of the priority in which it’s invoked.

    The workaround is to eliminate the new line characters:

    8.99

    <p class="product" style="border: 2px solid gray; padding: 12px;">8.99</p>

    So that’s what I’m going to ask WooCommerce to do; change the shortcode so that it doesn’t generate new line characters.

    Improve bw_better_autop?

    A better (more robust) solution may require one or more of the following:

    • a fix to wpautop()
    • another filter before wpautop() – remove unwanted new line characters generated during shortcode expansion
    • another filter after wpautop() – make sure p tags match
    • See if we can make use of the “no_texturize_shortcodes” filter logic

    I’m still investigating this.

    See also

    Stack overflow: Disable automatic formatting inside WordPress shortcodes Simon Battersby: Plugin to stop WordPress adding <br/> tags oik-plugins: bw_better_autop() WordPress undocumented “no_texturize_shortcodes” filter oik-css [bw_css] [bw_geshi]

    Further comments

    There are many documented bugs with wpautop(). Just do a search on “WordPress trac wpautop”. #2833 – wpautop breaks style and script tags (now 7 years old) seems to be the most pertinent!

    The explanation above probably applies to this problem: Trouble with WooCommerce shortcode add_to_cart

    Note: I had to use the Text editor to type this. Switching to Visual removes the p tags completely! That’s another issue.


    hover to slideToggle source


    , ,

    Published:

    Last updated:

    May 14, 2015

Categories

Tide times from tidetimes.co.uk

Tide Times & Heights for Langstone Harbour on
Monday, 29 November 2021
High Tide:06:29 ( 4.20m )
Low Tide:12:19 ( 1.80m )
High Tide:18:54 ( 4.10m )

Tide times from tidetimes.org.uk

Tide Times & Heights for Northney on
29th November 2021
06:48 High Tide ( 4m )
12:15 Low Tide ( 1.67m )
19:11 High Tide ( 3.92m )