Why this matters for your WordPress site
Most WordPress themes include a 404.php file.
If your does not it's likely that adding a custom 404.php file will simply work out-of-the-box.
Below is my recommended coding for your 404.php file.
First, duplicate your index.php file, then rename as: 404.php
Step-by-step setup and recommendations
Note where the body text section is located within the file and simply replace with this example below. Change the descriptive text and styling as needed:
<div id="content" class="narrowcolumn">
<h2>My_Domain<br />Our goal is to help...</h2>
<p>Call us anytime, yada yada</p><br />
<?php
$s = preg_replace("/(.*)-(html|htm|php|asp|aspx)$/","$1",$wp_query->query_vars['name']);
$posts = query_posts('post_type=any&name='.$s);
$s = str_replace("-"," ",$s);
if (count($posts) == 0) {
$posts = query_posts('post_type=any&s='.$s);
}
if (count($posts) > 0) {
echo "<ol><li>";
echo "<p>Were you looking for one of the following articles?</p>";
echo "<ul>";
foreach ($posts as $post) {
echo '<li><a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a></li>';
}
echo "</ul>";
echo "<p></p></li>";
} else {
echo "<p></p>";
echo "<ol>";
}
?>
<li>
<label for="s"><strong>Search</strong> for it:</label>
<form style="display:inline;" action="<?php bloginfo('siteurl');?>">
<input type="text" value="<?php echo esc_attr($s); ?>" id="s" name="s"/> <input type="submit" value="Search"/>
</form>
</li>
</ol>
</div>
Common mistakes to avoid
Note: this example uses query_posts(), which WordPress now discourages in modern themes. It still works on most sites, but test your 404 page after adding it by visiting a made-up address on your site (see the test URL below).
Then test your 404 page by going to http://your+domain/junktext
Suffice it to say, having a poorly coded or broken 404 page can cause a great deal of hurt SEO-wise if you don't do this right...
Enjoy!
