Automatically remove code mistakes in posts
create the function which will takes your post content, clean it and print or return it.
Copy the code below to the functions.php file from your theme. Create that file if it doesn’t exists.
function clean_bad_content( $bPrint = false ){
global $post;
$szPostContent = $post->post_content;
$szRemoveFilter = array( "~<p[^>]*>\s?</p>~", "~<a[^>]*>\s?~", "~<font[^>]*>~", "~<\/font>~", "~style\=\"[^\"]*\"~", "~<span[^>]*>\s?</span>~" );
$szPostContent = preg_replace( $szRemoveFilter, '' , $szPostContent);
$szPostContent = apply_filters('the_content', $szPostContent);
if ( $bPrint == false ) return $szPostContent; else echo $szPostContent;
}
Proceed as the following to display your clean post content:
<?php if ( function_exists( 'clean_bad_content' ) ) clean_bad_content( true ); ?>
The clean_bad_content() only takes one argument, a boolean to specify if you’d like to print the result (true) , or get it for further use in php (false).