Snippet

Remove inline styles on page render

/**
 * Remove inline styles on page render
 * ----------------------------------------------------------------------------
 */
add_filter( 'the_content', 'clean_post_content' );
function clean_post_content($content) {

    // Remove inline styling
    $content = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $content);

    // Remove font tag
    $content = preg_replace('/<font[^>]+>/', '', $content);
    
     // Remove bgcolor
    $content = preg_replace('/(<[^>]+) bgcolor=".*?"/i', '$1', $content);
    
    //Remove valign
    $content = preg_replace('/(<[^>]+) valign=".*?"/i', '$1', $content);

    // Remove empty tags
    $post_cleaners = array('<p></p>' => '', '<p> </p>' => '', '<p>&nbsp;</p>' => '', '<span></span>' => '', '<span> </span>' => '', '<span>&nbsp;</span>' => '', '<span>' => '', '</span>' => '', '<font>' => '', '</font>' => '');
    $content = strtr($content, $post_cleaners);

    return $content;
}