For the WordPress noobs, they are unfamiliar with the correct and proper spelling of WordPress, which is capital W and a capital P, all one word.
So how about adding a function into your theme that will auto correct any misspellings?
And now, the simple (redundant code)
function wpwag_spell_wordpress_correct($text) {
$text = str_replace('Wordpress', 'WordPress', $text);
$text = str_replace('wordpress', 'WordPress', $text);
$text = str_replace('wordPress', 'WordPress', $text);
$text = str_replace('word-press', 'WordPress', $text);
$text = str_replace('wOrdpRess', 'WordPress', $text);
$text = str_replace('wp', 'WordPress', $text);
$text = str_replace('WP', 'WordPress', $text);
return $text;
}
As you can see, we’re just copying multiple code, and trying to find all the matches in which one might write WordPress incorrectly.
So now, the more advances replacement
I’ve added to the function, so that it won’t replace wp inside of an image.
function wpwag_spell_wordpress_correct($text) {
$ignore = '/([^<>]+)< [^>]+>|< [^>]+>([^<>]+)/'; //image pattern
if ( preg_match_all($ignore,$text,$replace,PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE) ) {
return $text;
} else {
$pattern[] = '/\bword[- ]?press\b/i';
$pattern[] = '/\bWP\b/i';
$replace = 'WordPress';
//removed "foreach" as suggested by @utkarshkukreti here
//foreach ( $pattern as $patterns )
$text = preg_replace($pattern,$replace, $text);
return $text;
}
}
This code is using regular expression, and an array of patterns. The first is looking for words matching wordpress or word-press with case insensitive matches.
While the second is looking for case insensitive matches to WP.
Pretty simple so far
Now we need to add either of the above code to your theme’s function.php file, then add the necessary filter! To do so just add this line:
add_filter( 'the_content', 'wpwag_spell_wordpress_correct', 10, 1 );
Now, if you’d like, you can also replace incorrect spellings in other area’s of WordPress. For example just add in any of the following filters you’d like.
add_filter( 'comment_text', 'wpwag_spell_wordpress_correct', 10, 1 );
add_filter( 'the_title', 'wpwag_spell_wordpress_correct', 10, 1 );
Just to name a few
That’s it!
Now I’d also like to mention you can create this as a plugin, or even download the multiple plugins that are already out there that do this very thing.
- WordPress Auto Correct
- Ozh’ Correctly Spell WordPress
- It’s WordPress
If you’re interested in adding this function as a plugin, just use the code below..
< ?php //remove space between '<' and '?'
/**
* Plugin Name: WPWag's Spell WordPress Correctly
* Plugin URI: http://wpwag.com/tips-tricks/auto-change-wp-and-incorrect-wordpress-spellings/
* Description: Writes WordPress as it should be written. “WordPress”.
* Version: 0.2
* Author: Austin Passy
* Author URI: http://austinpassy.com/
*/
function wpwag_spell_wordpress_correct($text) {
/** advanced *don't match the following **/
$ignore = '/([^<>]+)< [^>]+>|< [^>]+>([^<>]+)/'; //image pattern
if ( preg_match_all($ignore,$text,$replace,PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE) ) {
return $text;
} else {
$pattern[] = '/\bword[- ]?press\b/i';
$pattern[] = '/\bWP\b/i';
$replace = 'WordPress';
//foreach ( $pattern as $patterns )
$text = preg_replace($pattern,$replace, $text);
return $text;
}
}
add_filter( 'the_content', 'wpwag_spell_wordpress_correct', 10, 1 );
?>