Creating a Custom 404 Page in WordPress - P4 - Meta Box + Breakdance
Let’s explore an interesting way to have flexible and meaningful content for a custom 404 page by using custom fields created with Meta Box and displaying them with Breakdance.
This is a custom 404 page I created as an example. The page may include any kind of content that you think is useful for the visitors.
Video version
Preparation
To make all the content on your 404 page dynamic and flexible, you should use custom fields to store them. In my case, they are the image, message, and button, as well as the link on the button. Then, you can change any of them to have a new 404 page.
So, these are tools we need in this practice.
- Meta Box core plugin: to have a framework to create custom fields. You can download it directly from wordpress.org;
- MB Settings Page: to create a settings page to input all the information that we want to be on the 404 page;
- Meta Box Builder: to have a UI on the back end to create custom fields to save that information visually;
- Meta Box Group (optional): to have your own structure of fields that fit your page’s content;
- Breakdance: to build the page.
Now, let’s go step by step.
1. Creating a new settings page
The 404 page is not a specific page as usual, so we’ll use a settings page to include all the content and settings for it.
Go to Meta Box > Settings Pages, and create a new settings page.
For the position of the settings page, you can put it on every place in the menu. We have the Menu Type section to set where to access the page. In this practice, I’ll set it as a submenu of the Settings menu.
The page is still blank now since we haven’t added any content to it.
We’ll do it in the next step.
2. Creating custom fields for the 404 page’s content
As I mentioned before, instead of adding any specific content to the 404 page, we’ll use custom fields to have dynamic content for it. These custom fields will be set on the created settings page.
Go to Meta Box > Custom Fields, and create a new field group.
These are some fields that I’ll create for the page as examples. Simply add more fields if you want to have more content on the page.
Just add field types one by one corresponding to the kind of content.
For the buttons, I’ll group the label and URL together. So, add a Group field for the button.
Inside the group, add two subfields with the corresponding field type.
In my specific case, I set the group as collapsible and named for the button.
If you want to add more than one button, make the group cloneable by enabling the corresponding option.
After having all the fields with reasonable settings, move to the Settings tab, set the Location as Settings page, and choose the created settings page to apply the fields to it.
Now, go to your settings page, and you will see custom fields displayed.
Just input content for the page there.
Next, we’ll get and display these input data on the frontend.
3. Creating a template for the 404 page
We’ll get and display the content of the 404 page in a template created with Breakdance.
Navigate to Breakdance > Templates, and add a new template.
Breakdance supports a specific template for the 404 page.
Just choose that option. Then, edit it in Breakdance.
Based on your desired 404 page, just add a new section and choose the suitable structure for it. In my case, I add a Column element, and select the two-column layout.
Next, just add some elements into two columns to get and display data for the 404 page.
Inside the first column, I’ll add an Image element.
Since it was saved in the custom field created with Meta Box, click on the dynamic data button to choose where we get data from.
Then, in the appeared pop-up, look for the Meta Box section, and choose the name of the field that we save the image.
In the second column, add a Text element for the message.
Also, use the dynamic data button, and select the field for the message.
The last one is the buttons. Their label and URLs are saved in the cloneable group. However, Breakdance hasn’t supported getting that kind of data in the settings page. So, we should use code to get them.
Add a Code Block element.
Then, add the following code:
<?php
$group = rwmb_meta( 'button', ['object_type' => 'setting'], '404-page' );
foreach ($group as $value) :
echo $value['title'];
echo $value['url'];
endforeach;
?>
Specifically:
$group = rwmb_meta( 'button', ['object_type' => 'setting'], '404-page' );
: is to get data from a group in the settings page through therwmb_meta()
function provided by Meta Box.button
: is the ID of the group.404-page
: is the ID of the settings page.foreach ($group as $value) … endforeach;
: is a loop to list all the items since the group is cloneable. Inside the loop, we’ll use the echo function to display data from two subfields with IDs astitle
andurl
.
That’s done for getting content.
Now, on the frontend, the data of the 404 page is displayed. But, the button is in the text format.
We need to modify it a little bit to have a better look. So, let’s move to the next step.
4. Styling the page
Back to edit the created template with Breakdance, change some settings in the Style tab of each element to have the desired look.
In the code block where we display the button, I also add some div
tags and classes. Also, modify it.
In there, to embed the URL on the label of the button, I modify the code like this:
<a class="mb_button" href="<?php echo $value['url'];?>"><?php echo $value['title'];?></a>
Finally, use CSS to style the button. You can add the CSS to the Code Block as well. There is no need to create another one.
And, this is the final look of my 404 page.
Then you will get a 404 page with beautiful, useful, and flexible content that you can change easily without touching the template anymore.
5. Updating the content for the 404 page
Somedays, if you want to update the image, message, or button on this page, just back to the settings page, and change the content in the custom fields. Then, you’ll have a 404 page with the new content.
It’s more convenient for management. This is exactly the strength of the dynamic content for the 404 page, as well as any page on your site.
You can change it regularly to find out which content will be most sufficient for your visitors.