Unlimited WordPress themes, plugins, graphics & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:13Length:1.8 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.10 Handling URLs That Are “Not Found”

An important part of creating a Bolt theme is deciding what a visitor will see if they visit a URL that doesn’t exist on the site. In this lesson you’ll learn two different ways of setting up “Not Found” content in your theme, and how to test what you make.

2.10 Handling URLs That Are “Not Found”

Hi, and welcome back to Bolt CMS Theme Development. In the last lesson, we finished off the last template file that we're going to be building for this course. And in this lesson we're gonna go through how to handle URLs that aren't found through your theme. Right now if you put in a URL that doesn't exist, this is what you're gonna see. Now the first thing that you want to do here is not panic, because don't worry, nobody else is actually going to see this page at all. This is a debugging page and it's only visible to people who are signed in as administrators. And that's important to realize because as you look through this debug page, you can see there's a lot of information about your installation and that is something you would not want to have publicly accessible. But don't worry, it's not publicly accessible, it's just there to help you with your development process. However, when you're trying to set up handling in your theme for URLs that are not found, this doesn't help you test what you're trying to do. So in order to allow you to see what visitors are going to see if they hit a URL that isn't there, you need to turn off debugging. So to do that, we're gonna go back into the config file that we have edited a couple times so far in the course. Now from here, hit Ctrl+F to run a find, and then search for debug. So here is the debug setting. I'm gonna change that to false. Now if we go back to our broken URL, now what we get is a fatal error. If you don't set up handling for pages that aren't found, this is going to be what your visitors will see though, so it is very important to make sure you include that as part of your Theme Development process. And there are a few ways you can go about setting up this handling and we're going to cover a couple of them. The easiest way to handle these URLs that aren't found is just by creating a page that has a particular slug and creating that in the back end of your Bolt site. So if we head back to the config file first, and we run a find for the not found setting. You can see here we have a slug provided. Now if we go into our site admin and we create a new page and give it the title not found, and give it a little bit of content. And then we will publish it. Now this page has the slug that is specified in our config file. So now, when we go to a URL that doesn't exist, there's our not found message. So let's just grabbed this page from here, and that's just using that to handle any URLs that don't exist. Now there's a couple problems with this, though. One, it's using the record template. So it's always gonna have the date that this was created on there and that's not really appropriate. It doesn't really fit what this page is supposed to do. And the other thing is if you look down here you have this Not Found page in the list of recent pages. So that is the easiest way to handle URLs that aren't found. And if you need to do something very quickly and you're not able to do anything else then you can fall back on that but it's not recommended for those reasons. So now what we're gonna do instead is we're gonna make a template to handle our URLs that aren't found instead of just relying on a page. So we'll head into our theme, And make a duplicate of the record template. And we'll rename it to Not-Found. And we don't have to do much with this because it's already set up almost perfectly. We just delete this paragraph here because we don't need the date found. And we'll delete the image because we also don't need that. And now rather than trying to pull anything in from a record, we're going to set our own text in here. So like with all the other texts that we've used so far, we want this to be translatable, so we put our two underscores and our brackets, and then we'll add a string for the title. And then we'll add another string for the body. Now, to make this template work, we need to jump back into our config file. And rather than having a slug here for a page, we're going to add in the name of our template. So we're going to put not-found.twig. So let's have a look at our broken URL again. And we should see this disappear. All right, so there we go. That's our not found twig file being pulled in instead of coming from this page. Now we can unpublish this page. And refresh. And now we also no longer see that not found page in our listing of recent pages. And then once you've done that and you've made sure that everything's being handled correctly, it's always a good idea to go back in here, find the debug setting again and turn it back on, set it to true. So now we are almost done. There's only one thing left to do for our theme and that is to enable live editing. Live editing is one of the coolest features that Bolt CMS offers. If you have a look in any of the records for any of the content types, you'll see this button here that says Live Edit. And this gives you a live display of exactly how the record that you're working on is going to look inside the site. However, right now it's not working because we haven't enabled it. So what we're going to do in the next lesson is we're going to enable editing on the title and on the body. So I'll see you in the next lesson.

Back to the top