I have this basic 404 page on my local machine.
I’ve edited my .htaccess:
ErrorDocument 404 /wordpress/wp-content/themes/test_theme/404.php
The 404 page does works and opens fine, but when I add my wp_head();
it breaks. The background-color still displays properly but the rest of the page fails.
<!DOCTYPE html>
<html lang='en-gb'>
<head>
<title>404 Error</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<link href='http://fonts.googleapis.com/css?family=Monoton' rel='stylesheet' type='text/css'>
<style type="text/css">
body {
background-color: #111;
}
.board {
position: absolute;
top: 50%;
left: 50%;
height: 150px;
width: 500px;
margin: -75px 0 0 -250px;
padding: 20px;
font: 75px/75px Monoton, cursive;
text-align: center;
text-transform: uppercase;
text-shadow: 0 0 80px red, 0 0 30px firebrick, 0 0 6px darkred;
color: red;
}
#error {
color: white;
text-shadow: 0 0 80px white, 0 0 30px green, 0 0 6px blue;
}
</style>
<?php wp_head(); ?>
</head>
<body>
<div class="board"><p id="error">error</p><p id="code">404</p></div>
<h2><?php _e( 'This is somewhat embarrassing, isnât it?', 'twentythirteen' ); ?></h2>
<p><?php _e( 'It looks like nothing was found at this location. Maybe try a search?', 'twentythirteen' ); ?></p>
<?php get_search_form(); ?>
</body>
</html>
I want to add some javascript via:
if(is_404()){ wp_register_script('404',get_template_directory_uri().'/js/jquery.novacancy.min.js', false, NULL, true);
wp_enqueue_script('404');
}
But adding wp_footer();
also breaks the page, it’s as though wordpress is breaking my 404 page.
The problem is that you are loading the theme file directly, which does not load all of the necessary WordPress files, and therefore functions. Therefore,
wp_footer();
andwp_header();
functions do not exists.WordPress automatically takes care of 404 errors, so remove the line you’ve added to your .htaccess file, and it will start working properly.
Be sure to read this article: Creating an Error 404 Page in WordPress
I think cale_b already answered you, but just to give more info, wp_head and get_footer is wordpress functions, so you can’t call them without calling get_header, or include in the wp_blog_header.php.
If you just need the functions without getting the whole header (probably you want a customized header, or no header at all), I would recommend including wp_blog_header.php at the very top of your php file, or at least before any wordpress functions.
wp_blog_header.php can be found at the root directory of the wordpress installation.