How to stop WordPress wrapping images in P tags

WordPress has a genius and at the same time annoying feature that automatically wraps inline elements in “P” tags for you. The nerd that wrote the function “wpautop” forgot to provide us an option to populate the array of defined block level elements in-case we wanted an image or perhaps even a span tag treated as a block level element and not wrapped in a P tag automatically.

After much research and not find a single straight forward answer that worked, I devised my own little solution. Basically what we have to do is create a new function called “wpautop_forked” inside of your theme functions.php file and then remove the annoying wpautop filter function and specifying our own instead. Here is the code below, function taken from the wp-includes file formatting.php for the latest version of WordPress at the time of writing 3.0.4. View the file and function on WordPress Trac here.

As you will see below all I did was add img to the faux array inside of the variable $allblocks. You can now make other inline elements be treated as block by WordPress as well by adding them with a pipe separator into the variable. The best part of about this approach is that we’re not hacking core files and I don’t think the wpautop function hardly ever gets changed. Dear WordPress development team nerds, fix this blatantly obvious issue. If I don’t want to abide by web standards to make complicated layouts a little easier, let me.

function wpautop_forked($pee, $br = 1) {