How To capitalize The First Letter Of Every Word In The Post Title

The code below was assembled of different pieces I’ve found around and it was not tested. Consider it as an idea only.

<?php
add_filter( 'the_title', 'my_capitalize_title', 10, 2 );

function my_capitalize_title( $title, $id ) {

    // get separate words
    $words = preg_split( '/[^\w]*([\s]+[^\w]*|$)/', $title, NULL, PREG_SPLIT_NO_EMPTY );

    $stop_words = array(
        'the', //
        'a',
        'and',
        'of',
    );

    $title_case="";

    foreach( $words as $word ) {
        // concatenate stop word intact
        if ( in_array( $word, $stop_words ) ) {
            $title_case .= $word;
        }
        // or concatenate capitalized word
        $title_case .=  ucfirst( $word );
    }

    return $title_case;
}

You have to polish the idea: you don’t want “The Simpsons” to become “the Simpsons”.