WordPress Fight

By | 2012-10-09

A quick note in case it helps someone else using WordPress. I had a bit of a fight trying to get WordPress to output the HTML that it was given for a post unaltered, this article records what I did.

I write these articles in markdown syntax, then convert them to HTML using pandoc. One advantage of pandoc is that it supports inline Latex math markup. Here’s a snippet from the source of an article:

 - $\frac{1}{2}$.  If half the roses are red, then half are not red.  Simple
   logic.  Well done amusing student.

The dash at the front is markdown for a bulleted list. The dollars switch to math mode. Here’s the HTML that pandoc converts that to (i.e. it’s what appears in the post editor box in the wordpress interface):

<li><math display="inline" xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mfrac><mn>1</mn><mn>2</mn></mfrac></mrow></math>.  If half
the roses are red, then half are not red. Simple logic. Well done
amusing student.</li>

This is perfect. Now here’s what WordPress was outputting (using “View Source” in a browser):

<li> <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML">
<mrow><mfrac><mn>1</mn><mn>2</mn></mfrac></mrow></math> <p>. If half
the roses are red, then half are not red. Simple logic.  Well done
amusing student.</li>

See it? WordPress has added a “<p>” all on its own. It’s completely unwanted, and it’s also unbalanced in that there is no closing “</p>”. It’s clearly a bug in WordPress. It took me a while to find a fix, and it’s not a real fix for the bug, but it’s all I needed.

It seems that WordPress has an output filter called “wpautop” that’s adding this spurious markup. If you disable it, then what you get out is what you put in – which is exactly what I want. I did that by editing the wp-includes/default-filters.php file and commenting out these lines:

add_filter( 'the_content', 'wpautop'            );

and

add_filter( 'the_excerpt', 'wpautop'          );

All my embedded maths is correctly formatted inline as part of the paragraph now (unless you’re viewing in IE, which doesn’t support MathML at all).

Perhaps this will help someone out there in the future.

Leave a Reply