Filter P-tags on wordpress images

Although good for separating the flow of your wordpress pages into neat chunks, wordpress’s habit of wrapping images in p-tags can also be restrictive.

Here is a code snippet I came across which strips the P-tags from wordpress images leaving you free to float your text and images left or right as you see fit. It has already been widely dispersed across the internet but just for good measure I will reproduce it here once again.

Add it anywhere in your functions.php file.

////// P-tag filter ////

	function ML_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', 'ML_filter_ptags_on_images');

Matthew

Share this

Leave a Reply

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