I used Divi to build the layout for a custom 404 page. I'm using this plugin https://wordpress.org/plugins/redirect-404-error-page-to-homepage-or-custom-page/ to redirect 404 errors to this page. It works, but when I run tools like screaming frog, it returns broken pages as 301 redirects, which I guess is expected.
What I would prefer however is for my 404.php template to include the page I built, so that I can still accurately track my 404 errors. I'm thinking of something like:
#404.php
<?php
require(/*Some function to get my entire custom 404 page by slug or ID*/;)
?>
What I tried:
//Redirect to our custom 404 page
function wf_404(){
//Check if custom 404 page exists to protect against infinite loop
if (is_404() && get_page_by_path('/404-page/', OBJECT)){
wp_safe_redirect(get_site_url() . '/404-page/', 404);
exit;
}
}
add_action('get_header', 'wf_404');
This gives me a general browser 404 error.
get_header()
call in your 404 template like any other page and then use the page structure from your regular page template file and instead of using the loop justecho apply_filters('the_content', $page->post_content);
where$page
is the results ofget_post('ID_of_your_page')
. It's not ideal as it requires hard coding the page ID into your template but it will get the job done if no one else has a better answer.