To achieve this recipe, you first have to creat 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?</a>~", "~<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).
Thanks to Matt Varone for this awesome function!
4 Responses
That could be very useful!
This is useful for blogs with multiple writers or for persons who don’t know HTML and use all sort of tools [programs] to write on their blog.
Sometimes doing stuff you don’t understand completely in a WYSIWYG blog editor may cause some damages to the look&feel of your blog
I had some “bad memories” with this kind of stuff from some of my team members from a blog of mine. Then, not knowing about this I had to edit the post and see where the errors were.
Trackbacks: