So yesterday, when I wrote my first blog post on this site, I found myself having to go back to my post two or three times because I had noticed that I had used the incorrect capitalisation on the word ‘WordPress’. Should be a capital W and P.

So in my warped mind, tonight I decided to write a wee plugin that will auto fix this issue whenever I publish or update a post. It’s probably the most straightforward pluginΒ you could hope to write but I like the exercise as you never know what you might learn on the way.

What I Did

In WordPress there is a FilterΒ calledΒ wp_insert_post_dataΒ this filter basically allows you to intercept the POST data from when the user presses ‘Publish’ or ‘Update’ on the CMS and edit the data in any way you want BEFORE the data is added to the database.

The syntax for running this filter looks like this:

function filter_handler( $data , $postarr ) {
  // do something with the post data
  return $data;

add_filter( 'wp_insert_post_data', 'filter_handler', '99', 2 );

So in my function I have basically done a find and replace and returned the new data for WordPress to add to the database.

function correct_capitalisation_WordPress( $data , $postarr ) {
  $correct_capitalisation = array();

  $incorrect_capitalisations = array(

  	'post_content' 	=> $data['post_content'],
  	'post_title' 	=> $data['post_title'],
  	'post_excerpt' 	=> $data['post_excerpt']


  foreach ($incorrect_capitalisations  as $key => $incorrect_capitalisation) {

    $correct_capitalisation[$key] = str_ireplace("WordPress", "WordPress", $incorrect_capitalisation);

  	$data[$key]  = $correct_capitalisation[$key];

  return $data;
add_filter( 'wp_insert_post_data', 'correct_capitalisation_WordPress', '99', 2 );

The Final Plugin:


Download Now