Build Your Own Business Website header image

How to Customize the 404 Error Page in Thesis

Difficulty Level -

Filed Under Topics -

Listed Under Lesson Subjects -

Whoops, you've found some protected content!

Watch the opening clip of this video to preview it,
the full video is available to free and paid members.

In this session we demonstrate how to customize the 404 Error Page in Thesis. We replace the default 404 Error message with our own customized 404 error message. The code for this session can be found below the video.

Code Used

* This function produces a custom 404 page
function custom_thesis_404_title() {
Oops, looks like you found one of our mistakes. We're sorry!
remove_action('thesis_hook_404_title', 'thesis_404_title');
add_action('thesis_hook_404_title', 'custom_thesis_404_title');

function custom_thesis_404_content() {
// Pull Query Variables and check for exact match
$s = htmlentities(strip_tags($_SERVER['REQUEST_URI']));
$s = preg_replace('/(.*)\.(html|htm|php|asp|aspx)?$/','$1',$s);
$posts = query_posts('post_type=any&name='.$s);

## If no posts match perform search ##
$s = str_replace('-',' ',$s);
if (count($posts) == 0) { $posts = query_posts('post_type=any&s='.$s);}

## Display results of either direct match or post search ##
<ol id="notfound">
if (count($posts) > 0) {
<li>Were you looking for <strong>one of the following</strong> posts or pages?
foreach ($posts as $post) {
<li><a href="<?php echo get_permalink($post->ID); ?>"><?php echo $post->post_title; ?></a></li>
If not, don't worry, here are a few more options:</li>
<li><strong>If you typed in an address�</strong> make sure the spelling, case, and punctuation are correct. Then try reloading the page.</li>
<li><strong>Search for it</strong> using the search box to the right.</li>
<li><strong>Look</strong> for it in the <a href="/sitemap">sitemap.</a></li>
<li><strong>Start over again</strong> at the <a href="/">homepage</a> (and please <a href="/about_byob/">contact me</a> to say what went wrong, so I can fix it).</li>
remove_action('thesis_hook_after_content', 'thesis_post_navigation');
remove_action('thesis_hook_404_content', 'thesis_404_content');
add_action('thesis_hook_404_content', 'custom_thesis_404_content');

Video Transcript

Rick: Good morning John

John: Hey, Rick. I’m just tuning in to check on the 404 error code.

Rick: You bet, okay. So here’s the code that I use on my site. WP content, themes, thesis_182. I’ve not upgraded BYOB website to thesis_184 yet. And then custom and then that is under templates. As I recall, it’s at the very bottom of templates. It is. Okay so let’s see, I’m just going to take it a little step by step to the code and then I’m going to post this code in the forum under Live Answer code so you can just get it yourself.

John: Great. Thank you.

Rick: Okay but the way this works is there’s a Thesis hook 404 content. And so what we do is we remove this Thesis 404 content from that hook and then we add our own content to that hook.

John: Sure.

Rick: Okay so here’s our content. The 404 title which is you know, I said, “Oops, it looks like you found one of my mistakes. We’re sorry.” Actually, this is 2 functions, I forgot because we also… you can change the title and you can change the content. This one changes the title. This one changes the content. And what happens here when it’s changing the content is it’s defining a search parameter and what it’s doing is it captures the text that you used to type in. For example, I used fish sticks last time, right?

John: Right.

Rick: It captures that text. It gets rid of all the extraneous pieces of code. So all it has is now… now all it has is the slug that’s left over. And then it queries posts of any kind of post name, post type with a name that is the same as that slug. So if it doesn’t show up exactly like that slug then it takes out the hyphens and just leaves spaces. And it looks for that series of terms in any post type and if it finds that series of terms then it displays that post. If it can’t find that, it either displays that it’s… oh no, I’m sorry. What it does is that returns a series of post types that has that collection of terms in it. And then what it does here is it says you know, “Were you looking for one of these other pages?” for example and then it lists out all those pages that has that same series of terms in it that you can choose from. If that series of terms doesn’t show anything then it says, “Oops, I’m sorry. We don’t have anything here. But if you would you know, you can either send me an email or you can you know, search or you can go to our site map or just go to our homepage you know, or contact me, actually.” So it does all those things.

John: And when it says post up there when it’s doing that query for any name, that also includes its searching pages as well as posts?

Rick: Yeah that’s why its post type is equals any because it’s going to check all post types, not just posts. In my case, I have some custom post types so it would check posts, pages, video posts, events you know, all those things.

John: Okay, post is kind of a generic term there, not related to blog posts per se.

Rick: Well, post type is the term for the name of a kind of content. So a post is a post type. A page is a post type. A custom post type is a post type.

John: I got you. Okay, I’ll look in the forum for that. Thank you.

Rick: Well, I’m just going to do that right now actually so I don’t forget. As soon as I’m done here, we are climbing in the car and driving this book in which is 5 hours away. So I just want to get this done while I can here.

John: Well, that’s great.

Rick: So let’s see…

John: And then I’m…

Rick: Go ahead.

John: Well, I was going to say if I wanted to make that simpler and not have the search engine just say you know, “Change it from you’d gnarled it or gnarling it…”

Rick: Well, you could just change the title if you wish because that gnarly dude is just the title thing.

John: Exactly and that hook could…

Rick: Right.

John: In the Thesis 404 hook.

Rick: Yes. We’ll just paste it here and then we’ll go back and take a look at it.

John: Okay.

Rick: Okay so I pasted the code, go down to the bottom of the code, go to the top of the code and wrap it in those code tags. Php and then say post new topic. Okay and then so looking at this code, the first one is just the title and that is Thesis hook 404 title.

John: I got you, okay.

Rick: So the thing that says “gnarly dude” that’s a function called Thesis 404 title. So you would remove that, the existing title from the existing title hook and you would add your new title to the existing title hook.

John: Okay. That’s the simplest way to go about it.

Rick: Absolutely. That is the very simplest solution right there.

John: Right, I got that. And now you’ve given me another solution where it’s more involved and the customer could… I mean, the visitor could search and be given options etcetera which is actually very interesting.

Rick: Yes. And so this code has both of those things in it.

John: Okay, that’s perfect. Thank you for that. I was also curious, I apparently hadn’t tuned in enough and I missed the first couple of minutes of today’s session. The instruction I found very helpful today, on Wednesdays, do you do a little bit of instruction and Q&A? It’s not totally Q&A?

Rick: Well, it depends. See, up until 4 o’clock this morning, I didn’t have any questions for today. So rather than sit around and twiddle our thumbs, I prepare something to teach then. And then at 4 o’clock this morning, people started asking questions and well, I was… I wanted to teach what I was preparing to teach because it was something that people had asked and so I presented that. You know, up until recently, there have not been enough questions to fill the time.

John: I see.

Rick: And so I have… I generally have something prepared to teach if there’s not enough time.

John: Not enough questions.

Rick: Not enough questions, right. And I usually you know, one of our members asked me how to convert a PSD into a website and so that was why I did this session. I usually pick what I’m going to teach based on what somebody else has said they need to understand and it’s not a quickie answer.

John: Sure. That makes sense. Well, that was very helpful. I learned a lot and it’s a perfect way to see how our graphic designer kind of envisioned her site and then for you to take the different layers and take them apart and place them in different sections in the site. It was very, very interesting.

Rick: Well, good.

John: I thoroughly enjoyed that.

Rick: Well, we’re going to take that on next week and then I think that the next course I teach is going to be how to create a Thesis child theme and we’re going to be doing that off of a PSD file again like that so there’ll be a whole bunch of that kind of thing going on then.

John: Okay, that’s great.

Rick: Okay?

John: One last quick question. I’m curious, when you use Firebug, you seem to be able to get a column on the left that shows the different stylesheets?

Rick: This is Web Developer. Have you watched the sections of Customize Thesis Like a Pro that talk about the tools of the trade?

John: No.

Rick: Okay well, you should go watch that because I show you know, what Web Developer does and how to use it in that.

John: Okay.

Rick: Web Developer’s an essential part of my workflow. I have to use Web Developer as part of my workflow.

John: And is that a plugin?

Rick: Yes, it’s another… it’s just like Firebug. It’s an addon to Firefox.

John: Oh okay. I thought they were together but that’s something else. Okay, great.

Rick: No. They’re related but they’re not the same.

John: I’ll check that out. Rick, thank you again and I’ll want to see you tomorrow night.

Rick: You’re welcome. Take it easy, John.

John: Thank you.

0 Comments… add one

Save $200 on Membership Now!

Start learning today for as little as
$0.82 PER DAY!
Subscription Options
0 comments… add one