Getting images to float without extra margin or page breaks

By default the WordPress visual editor will ad ‘p’ tags to all of your text and images. When you are floating an image the p tag around it can create more margin than you want, and it often ads an extra ‘br’ tag to the code which can cause spacing problems.

Add the following code to the functions.php file to prevent the IMG tags from being wrapped in a p tag.

Thanks to Css-Tricks.com for this code. css-tricks.com/snippets/wordpress/remove-paragraph-tags-from-around-images/

function filter_ptags_on_images($content){
   return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}

add_filter('the_content', 'filter_ptags_on_images');

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.